sql -leetcode 178. Rank Scores
阿新 • • 發佈:2017-07-31
sql 3.6 group by img core select brush distinct pre
Score 很好得到: select Score from Scores order by Score desc;
要得到rank, 可以通過比較比當前Score 大的Score 的個數得到:
select Score, (select count(distinct Score) from Scores where Score>=s.Score) Rank where Scores s order by Score desc;
或者:
select s.Score ,count(distinct t.Score)Rank from Scores s join Scores t on s.Score<=t.Score
得到join 兩個表,
{"headers": ["Score", "Score"], "values": [[3.50, 3.50], [3.50, 3.65], [3.65, 3.65], [3.65, 3.65], [3.50, 4.00], [3.65, 4.00], [4.00, 4.00], [3.85, 4.00], [4.00, 4.00], [3.65, 4.00], [3.50, 3.85], [3.65, 3.85], [3.85, 3.85], [3.65, 3.85], [3.50, 4.00], [3.65, 4.00], [4.00, 4.00], [3.85, 4.00], [4.00, 4.00], [3.65, 4.00], [3.50, 3.65], [3.65, 3.65], [3.65, 3.65]]}
按s.Id 聚在一起:
select s.Score ,count(distinct t.Score)Rank from Scores s join Scores t on s.Score<=t.Score group by s.Id order by s.Score desc
sql -leetcode 178. Rank Scores