Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Other Forums
 Other Topics
 SQL: Top N rows for each X

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2005-08-10 : 07:47:04
Umed writes "I want to retrive data from a table say TABLE1(ID,NAME).

Table has following data:

ID NAME
1 A
1 B
1 C
1 H
2 R
2 T
2 U
2 P
2 J
3 L
3 J
4 G
5 W
5 Q
5 S


I want to select TOP N (say N=3) rows for each ID (1,2,3,4,5),
i.e. output would be like

ID NAME
1 A
1 B
1 C
2 R
2 T
2 U
3 L
3 J
4 G
5 W
5 Q
5 S


SQL query should not be specific to oracle or SQL Server or other DB, but a general one...

Thnx."

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2005-08-10 : 07:54:34
http://weblogs.sqlteam.com/mladenp/archive/2005/08/01/7421.aspx
number 2.

Go with the flow & have fun! Else fight the flow
Go to Top of Page
   

- Advertisement -