SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Help Needed with TSQL Query


Help Needed with TSQL Query

Author
Message
Sri8143
Sri8143
Mr or Mrs. 500
Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)

Group: General Forum Members
Points: 529 Visits: 2469
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
ColdCoffee
ColdCoffee
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3713 Visits: 5549
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.
Sri8143
Sri8143
Mr or Mrs. 500
Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)

Group: General Forum Members
Points: 529 Visits: 2469
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
ColdCoffee
ColdCoffee
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3713 Visits: 5549
My code exactly does that. Does it not? Can you please tel me what error you are seeing with my code?
Sri8143
Sri8143
Mr or Mrs. 500
Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)

Group: General Forum Members
Points: 529 Visits: 2469
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
ColdCoffee
ColdCoffee
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3713 Visits: 5549
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.,,,
Sri8143
Sri8143
Mr or Mrs. 500
Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)

Group: General Forum Members
Points: 529 Visits: 2469
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
ColdCoffee
ColdCoffee
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3713 Visits: 5549
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 :-)
Sri8143
Sri8143
Mr or Mrs. 500
Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)Mr or Mrs. 500 (529 reputation)

Group: General Forum Members
Points: 529 Visits: 2469
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
ColdCoffee
ColdCoffee
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3713 Visits: 5549
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?
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search