SQLTeam.com | Weblogs | Forums

Ranking Data


#1

Hello -

I have written a query to pull all claims for a certain account. Now I would like to rank them so that I get the top 10 but I dont know how to do that. Can anyone please help me?

Thanks!

John


#2
SELECT TOP (10)
   col1, col2, col3
FROM
   YourTableOrQuery
ORDER BY
   ClaimAmount DESC -- Or whatever your sorting criteria is

#3

Ahhhhh, thats easy!

Lets say my columns are name, id, amount and I want the top 10 based on amount only. Can I or do I need to specify that, James?

Thank you!
John


#4

To get the top 10 based on amount (i.e., the highest amounts), your ORDER BY clause would be ORDER BY Amount DESC.

If there happens to be ties - e.g., there are two entries with the same amount that compete for the tenth place -and if you want to get all the ties, change the TOP (10) to TOP (10) with TIES. When you do that, of course, you might get more than 10 rows.


#5

Thank you, sir! I appreciate the help......