Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Help Needed with TSQL Query Expand / Collapse
Author
Message
Posted Monday, August 5, 2013 11:55 PM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, August 18, 2014 10:42 PM
Points: 386, Visits: 2,345

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 ..

[code]
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'

[code]


Thanks,
Chinna

Its the Journey which gives you Happiness not the Destination-- Dan Millman
Post #1481160
Posted Tuesday, August 6, 2013 12:57 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, July 22, 2014 9:03 PM
Points: 2,262, Visits: 5,405
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.
Post #1481171
Posted Tuesday, August 6, 2013 5:19 PM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, August 18, 2014 10:42 PM
Points: 386, Visits: 2,345
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
Post #1481615
Posted Tuesday, August 6, 2013 10:19 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, July 22, 2014 9:03 PM
Points: 2,262, Visits: 5,405
My code exactly does that. Does it not? Can you please tel me what error you are seeing with my code?
Post #1481640
Posted Tuesday, August 6, 2013 10:40 PM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, August 18, 2014 10:42 PM
Points: 386, Visits: 2,345
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
Post #1481643
Posted Tuesday, August 6, 2013 11:12 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, July 22, 2014 9:03 PM
Points: 2,262, Visits: 5,405
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.,,,
Post #1481648
Posted Tuesday, August 6, 2013 11:52 PM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, August 18, 2014 10:42 PM
Points: 386, Visits: 2,345
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
Post #1481661
Posted Wednesday, August 7, 2013 12:02 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, July 22, 2014 9:03 PM
Points: 2,262, Visits: 5,405
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
Post #1481666
Posted Wednesday, August 7, 2013 12:13 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, August 18, 2014 10:42 PM
Points: 386, Visits: 2,345
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
Post #1481671
Posted Wednesday, August 7, 2013 12:21 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, July 22, 2014 9:03 PM
Points: 2,262, Visits: 5,405
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?
Post #1481676
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse