|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. fun2oos |
|
#2
|
|||
|
|||
|
SELECT * FROM TABLE1 t1
WHERE [NAME] IN ( SELECT TOP 3 [NAME] FROM TABLE1 t2 WHERE t1.[ID] = t2.[ID] order by [ID] DESC ) order by t1.[ID] urs solution is welcome |
![]() |
| Viewing: ASP Free Forums > Database > Microsoft SQL Server > SQL: Top N rows for each X |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|