Help Needed with TSQL Query

  • I have two diff tables TBL1 and TBL2 in which they have mapping on CustNo and CustId ..TBL1 is the parent table and TBL2 has the details..

    TBL1 can have more than one CID per each CNo and I need to write a query to get one Cid per CNo and the logic for that is the more number of records per each cid in TBL2 in the last 30 days.

    Lets say in the example below we have

    Cno Cid Cnt

    10--> 1 --> 3

    10--> 2-- > 4

    So the O/P should be

    10--> 2(Max no of records) If nothing in the last 30 days then take the last record available for that CNo

    Can someone help me the easiet way to do it ..

    CREATE TABLE #temp

    ( CNO INT NOT NULL,

    CId int NOT NULL

    )

    INSERT INTO #temp

    SELECT 10 , 1

    UNION

    SELECT 10,2

    UNION

    SELECT 11 , 1

    UNION

    SELECT 12,2

    UNION

    SELECT 13 , 1

    UNION

    SELECT 14,2

    UNION

    SELECT 14 , 1

    UNION

    SELECT 15,2

    SELECT * FROM #temp1

    CREATE TABLE #temp1

    ( CNo INT NOT NULL,

    CId INT NOT NULL ,

    CDate DATETIME NOT NULL

    )

    INSERT INTO #temp1

    SELECT 10,1,'2013-06-07 15:33:40.043'

    UNION

    SELECT 10,1,'2013-07-07 15:33:40.043'

    UNION

    SELECT 10,1,'2013-08-05 15:33:40.043'

    UNION

    SELECT 10,2,'2013-07-31 15:34:03.857'

    UNION

    SELECT 10,2,'2013-08-03 15:34:03.857'

    UNION

    SELECT 10,2,'2013-08-04 15:34:03.857'

    UNION

    SELECT 10,2,'2013-08-02 15:34:11.027'

    Thanks,
    Chinna

    Its the Journey which gives you Happiness not the Destination-- Dan Millman

  • How about this?

    ; WITH RankedListed (CNo, CId, [Rank]) AS

    (

    SELECT T.CNO

    ,T.CId

    ,ROW_NUMBER()

    OVER ( PARTITION BY T.CNO

    ORDER BY CASE WHEN COUNT(*) = 1 THEN T.CId

    ELSE COUNT(*)

    END

    DESC) AS 'Rank'

    FROM #temp T

    LEFT JOIN #temp1 T1

    ON T.CNO = T1.CNo

    AND T.CId = T1.CId

    GROUP BY T.CNO

    ,T.CId

    --ORDER BY T.CNO

    --,T.CId

    )

    SELECT RL.CNo , RL.CId

    FROM RankedListed RL

    WHERE RL.[Rank] = 1

    Thanks for posting the readily-consumable test data.

  • Firstly Thanks a lot for the response ..

    Like I mentioned I need to check the records created only in the last 30 days and find out the max count for each Cid and if there are no records in the last 30 days then I should get the last transaction based on datestamp if nothing then based on CID desc

    Now in the below example I don't have any records for CNo in the last 30 days so I need to get the last record CId else the Higher number of CId for each CNo

    O/P

    10--> 2

    11--> 1

    12--> 2

    13--> 1

    14--> 2

    15--> 3

    Is there a way to get the below result in single query or should we be creating multiple temp tables and do the calc..

    CREATE TABLE #temp

    ( CNO INT NOT NULL,

    CId int NOT NULL

    )

    INSERT INTO #temp

    SELECT 10 , 1

    UNION

    SELECT 10,2

    UNION

    SELECT 11 , 1

    UNION

    SELECT 12,2

    UNION

    SELECT 13 , 1

    UNION

    SELECT 14,2

    UNION

    SELECT 14 , 1

    UNION

    SELECT 15,2

    UNION

    SELECT 15,3

    SELECT * FROM #temp1

    CREATE TABLE #temp1

    ( CNo INT NOT NULL,

    CId INT NOT NULL ,

    CDate DATETIME NOT NULL

    )

    INSERT INTO #temp1

    SELECT 10,1,'2013-05-07 15:33:40.043'

    UNION

    SELECT 10,1,'2013-06-07 15:33:40.043'

    UNION

    SELECT 10,1,'2013-06-05 15:33:40.043'

    UNION

    SELECT 10,2,'2013-06-31 15:34:03.857'

    UNION

    SELECT 10,2,'2013-06-03 15:34:03.857'

    UNION

    SELECT 10,2,'2013-06-04 15:34:03.857'

    UNION

    SELECT 10,2,'2013-06-02 15:34:11.027'

    Thanks in advance ..

    Thanks,
    Chinna

    Its the Journey which gives you Happiness not the Destination-- Dan Millman

  • My code exactly does that. Does it not? Can you please tel me what error you are seeing with my code?

  • Lets insert few more records into the #temp1 table in addition to the existing one..

    INSERT INTO #temp1

    SELECT 11,1,'2013-07-17 15:33:40.043'

    UNION

    SELECT 11,1,'2013-07-27 15:33:40.043'

    UNION

    SELECT 11,1,'2013-08-05 15:33:40.043'

    UNION

    SELECT 11,2,'2013-07-30 15:34:03.857'

    UNION

    SELECT 15,2,'2013-06-03 15:34:03.857'

    UNION

    SELECT 10,2,'2013-06-04 15:34:03.857'

    UNION

    SELECT 10,2,'2013-06-02 15:34:11.027'

    now when you execute your query all I would get is for the Cno 15 you will Cid 3 but as per the logic it should be 2 ..

    Logic..

    1. More no of records created in the last 30 days fpr each Cno if not found

    2. Get the last record created it need not be in the last 30 days

    3. If no record available get the highest number..

    The query doesn't work when I put the date filter ..

    LEFT JOIN #temp1 T1

    ON T.CNO = T1.CNo

    AND T.CId = T1.CId

    AND CDate >= GETDATE() -30

    Thanks,
    Chinna

    Its the Journey which gives you Happiness not the Destination-- Dan Millman

  • I see your point now

    ; WITH RankedListed (CNo, CId, [Rank]) AS

    (

    SELECT T.CNO

    ,T.CId

    ,ROW_NUMBER()

    OVER ( PARTITION BY T.CNO

    ORDER BY CASE WHEN COUNT(T1.CId) = 1 THEN T.CId

    ELSE COUNT(T1.CId)

    END

    DESC) AS 'Rank'

    FROM #temp T

    LEFT JOIN #temp1 T1

    ON T.CNO = T1.CNo

    AND T.CId = T1.CId

    GROUP BY T.CNO

    ,T.CId

    --ORDER BY T.CNO

    --,T.CId

    )

    SELECT RL.CNo , RL.CId

    FROM RankedListed RL

    WHERE RL.[Rank] = 1

    try that and let me know. It does not have the 30 day only rule yet. I am working on it and will update the thread as and wehen ready.,,,

  • I really did not understand the query why you have Count(T1.CID) = 1 then T.CID in the order by clause.. I have rewritten the code to below by changing the order by clause

    ..tested with few scenarios and seems to work ..Still trying to figure out what was the intention of having count(T.Cid) = 1.. Am I missing something here ?

    Trying to work on the Date logic to fit into this

    ; WITH RankedListed (CNo, CId, [Rank]) AS

    (

    SELECT T.CNO

    ,T.CId

    ,ROW_NUMBER()

    OVER ( PARTITION BY T.CNO

    ORDER BY COUNT(T1.CId) DESC , T.CID

    DESC) AS 'Rank'

    FROM #temp T

    LEFT JOIN #temp1 T1

    ON T.CNO = T1.CNo

    AND T.CId = T1.CId

    GROUP BY T.CNO

    ,T.CId

    --ORDER BY T.CNO

    --,T.CId

    )

    SELECT RL.CNo , RL.CId

    FROM RankedListed RL

    WHERE RL.[Rank] = 1

    Thanks,
    Chinna

    Its the Journey which gives you Happiness not the Destination-- Dan Millman

  • You dint understand the part ONLY with the count(*)/count(T1.CiD) or the entire query?

    Count(*) yielded the number of rows for CNo. But Count(T1.CiD) will yield the number of rows present in the #temp1 table pertaining to CNo. This made the difference.

    As far as the date logic goes, i will be working that up tomo, as i have some other stuff running here 🙂

  • Thanks a lot in advance for the help..

    The part I don't understand is just the count(T1.Cid) = 1 ( why 1 )...I did understand rest of the query and was able to do changes so that it works as needed for my query ..

    When there are two Cids and the count for both records are 2 then the query you provided has given me the diff ranking than what I want ...

    Eg:

    15 -- > 3 --> 2 -- Rank 2

    15 -- > 2 --> 2 -- Rank 1

    Then its ranking the second record as 1 but when its tie the tie breaker is need to take the highest Cid Number record. I believe the query which I have re-written based on ur query does that .. It could be my bad I might not have explained the full view of the req.

    Its just that I need to work on the date part how to fit it into this above query

    Thanks,
    Chinna

    Its the Journey which gives you Happiness not the Destination-- Dan Millman

  • My query, without chaning anything, works for tie-breakers too as per your rule (highest of cid should be retrieved). Can you give me a perfect set of example to work on? A sample data set that covers all your business rules?

  • If you just insert the below records into temp1 and run your query its not ranking as expected for the Cn0--15 .. I believe the other query which I made changes will do cover all my rules.. The only other help I might need is to know how to fit the date rule applied into this query ..

    INSERT INTO #temp1

    SELECT 15,2,'2013-07-27 16:33:40.043'

    UNION

    SELECT 15,2,'2013-07-27 15:33:40.043'

    UNION

    SELECT 15,3,'2013-08-05 15:33:40.043'

    UNION

    SELECT 15,5,'2013-07-30 15:34:03.857'

    UNION

    SELECT 11,2,'2013-06-03 15:34:03.857'

    UNION

    SELECT 10,2,'2013-06-04 15:34:03.857'

    UNION

    SELECT 10,2,'2013-06-02 15:34:11.027'

    ; WITH RankedListed (CNo, CId, [Rank]) AS

    (

    SELECT T.CNO

    ,T.CId

    ,ROW_NUMBER()

    OVER ( PARTITION BY T.CNO

    ORDER BY CASE WHEN COUNT(T1.CId) = 1 THEN T.CId

    ELSE COUNT(T1.CId)

    END

    DESC) AS 'Rank'

    FROM #temp T

    LEFT JOIN #temp1 T1

    ON T.CNO = T1.CNo

    AND T.CId = T1.CId

    GROUP BY T.CNO

    ,T.CId

    --ORDER BY T.CNO

    --,T.CId

    )

    SELECT RL.CNo , RL.CId

    FROM RankedListed RL

    WHERE rank = 1

    Thanks,
    Chinna

    Its the Journey which gives you Happiness not the Destination-- Dan Millman

  • Can you just remove the T.Cid form the THEN clause and replace it with 0 (zero) and try?

    ORDER BY CASE WHEN COUNT(T1.CId) = 1 THEN 0

    ELSE COUNT(T1.CId)

    END

    DESC

    Most of your business rules will be messed if you remove he CASE statement. So be careful in removing it.

  • Okay .. Will try that and let you know ..

    Can you guide me in what way can the date fit in .. Should we be moving data to diff table to get the desired result ?

    Thanks,
    Chinna

    Its the Journey which gives you Happiness not the Destination-- Dan Millman

Viewing 13 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic. Login to reply