Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reordering the top results

Status
Not open for further replies.

notbrain

Programmer
Aug 28, 2006
1
US
Lets say I have a table:

[tt]
id | name | count
-----------------
1 a 25
2 b 24
3 c 23
4 d 20
5 e 16
6 f 14
7 g 12
8 h 12
9 i 4
10 j 2
[/tt]

Is it possible, in one query, to order the TOP 5 counts in ASCENDING order by that same count? The result I'm looking for is:

[tt]
id | name | count
-----------------
5 e 16
4 d 20
3 c 23
2 b 24
1 a 25
[/tt]

I tried to use a count2 alias of count and then [tt]ORDER BY count DESC, count2 ASC[/tt], to no avail.

Thanks in advance!







 
Like this ?
SELECT A.ID, A.Name, A.Count
FROM yourTable A INNER JOIN yourTable B ON A.Count <= B.Count
GROUP BY A.ID, A.Name, A.Count
HAVING COUNT(*) <= 5
ORDER BY 3

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top