Select Top 5

L

Luis

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
 
D

Dirk Goldgar

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;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top