首页 > 代码库 > sql -leetcode 178. Rank Scores
sql -leetcode 178. Rank Scores
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.Scoregroup by s.Idorder by s.Score desc
sql -leetcode 178. Rank Scores
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。