Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Hi,

I have 2 columns in my table, ID and Report.

Report has 5 categories: Desktop, Laptop, Server, Monitor and Overall

Each category has 5 rows.

I need top three IDs from each category.

Please help.

Thanks in advance.
Posted

 
Share this answer
 
It can be achieved by this.


C#
with ID as 
(SELECT id, 
report_type, ROW_NUMBER() OVER(PARTITION BY report_type ORDER BY id DESC)
 as 'RowNum' from tablename)
SELECT id,report_type FROM ID WHERE RowNum <= 3
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900