LeetCode 178. Rank Scores
考点 | 难度 |
---|---|
Database | Medium |
题目
Write an SQL query to rank the scores. The ranking should be calculated according to the following rules:
The scores should be ranked from the highest to the lowest.
If there is a tie between two scores, both should have the same ranking.
After a tie, the next ranking number should be the next consecutive integer value. In other words, there should be no holes between ranks.
Return the result table ordered by score
in descending order.
思路
用s2
计算小于s.score
的数量。
答案
select s.score, count(s2.score) as `Rank` from
(select distinct score from scores) s2, scores s
where s.score<= s2.score
group by s.id
order by s.score desc;