简体   繁体   中英

SQL Server query, in order of higher number in a column

How would I write a query that only shows 10 pieces of data and only the highest number in the count column?

I'm thinking,

SELECT * 
FROM score 
WHERE count = (SELECT MAX(count) 
               FROM score 
               WHERE count <> (SELECT MAX(count) FROM score) TOP 10;

I know I'm almost there just not quite.

I am trying to show the top ten of the highest count from the score table.

If you have the scores and just want to see them in order:

SELECT 
  top 10 count 
FROM 
  score
order by count desc

To use an aggregate, you need to compare it against something. If you have users for example:

SELECT 
  top 10 sum(count), u.userid
FROM 
  score s
  inner join users u on s.userid = u.userid
group by u.userid
order by sum(count) desc

SELECT * FROM SCORE ORDER BY COUNT DESC TOP 10

Or wherever top 10 goes... will give top 10 scores.

SELECT * FROM SCORE WHERE COUNT = (SELECT MAX(COUNT) FROM SCORE) TOP 10

Top 10 rows having score equal max count.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM