July 5, 2016 at 5:34 am
Hi,
Please try using following code-
DECLARE @T TABLE (ID INT, NAME VARCHAR(1))
INSERT @T VALUES (1000,'A'),(990,'C'),(980,'A'),(970,'A'),(960,'B'),(950,'C'),(930,'A')
SELECT * FROM(SELECT ROW_NUMBER() OVER( PARTITION BY NAME ORDER BY ID DESC)ROWNUM,ID,NAME FROM @T )T
WHERE T.ROWNUM<=3
Regards,
Anand
July 5, 2016 at 5:42 am
shirolkar.anand (7/5/2016)
Hi,Please try using following code-
DECLARE @T TABLE (ID INT, NAME VARCHAR(1))
INSERT @T VALUES (1000,'A'),(990,'C'),(980,'A'),(970,'A'),(960,'B'),(950,'C'),(930,'A')
SELECT * FROM(SELECT ROW_NUMBER() OVER( PARTITION BY NAME ORDER BY ID DESC)ROWNUM,ID,NAME FROM @T )T
WHERE T.ROWNUM<=3
Regards,
Anand
This doesn't work, the names are in incorrect order.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Viewing 3 posts - 16 through 17 (of 17 total)
You must be logged in to reply to this topic. Login to reply