• 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.Score
    group by s.Id
    order by s.Score desc
    

      







  • 相关阅读:
    第一周作业
    第一周作业
    C语言I博客作业07
    C语言I博客作业06
    C语言I博客作业03
    C语言I博客作业04
    c语言|博客作业02
    字段的约束验证
    [转]AS IS ? ??运算符
    BindingManagerBase 跟踪不一致
  • 原文地址:https://www.cnblogs.com/fanhaha/p/7264708.html
Copyright © 2020-2023  润新知