Select Top 5

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello.
I have a select query that returns a list of records of the following kind:

Field1 Field2
A 8
A 7
A 5
A 4
A 3
A 2
B 10
B 9
B 8
B 7
B 6
B 5
........

Is it possible to return only the top 5 Values of Field2 for each value of
Field1 ?

Thanks.

Luis
 
In
Luis said:
Hello.
I have a select query that returns a list of records of the following
kind:

Field1 Field2
A 8
A 7
A 5
A 4
A 3
A 2
B 10
B 9
B 8
B 7
B 6
B 5
.......

Is it possible to return only the top 5 Values of Field2 for each
value of Field1 ?

Something along these lines, I think:

SELECT DISTINCT Field1, Field2 FROM YourTable
WHERE YourTable.Field2 In
(SELECT TOP 5 T.Field2 FROM YourTable T
WHERE T.Field1 = YourTable.Field1
ORDER BY T.Field2 DESC)
ORDER BY Field1, Field2 DESC;
 
Back
Top