SQL 87 ) 最差是第几名(一)

最差是第几名(一)

http://www.nowcoder.com/questionTerminal/ae5e8273e73b4413823b676081bd355c

用转置函数 case when 解决

select grade ,
case grade when 'A' then (select sum(number) from class_grade where grade <= 'A')
            when 'B' then (select sum(number)from class_grade where grade <= 'B')
            when 'C' then (select sum(number)from class_grade where grade <= 'C')
            when 'D' then (select sum(number)from class_grade where grade <= 'D')
            else (select sum(number) from class_grade)
    end
from class_grade
order by grade

用窗口函数sum()over()解决

select grade, sum(number)over(order by grade asc)t_rank
from class_grade
order by grade
SQL 文章被收录于专栏

SQL

全部评论

相关推荐

牛客868257804号:九个中铁八个中建
点赞 评论 收藏
分享
牛客963010790号:为什么还要收藏
点赞 评论 收藏
分享
10 1 评论
分享
牛客网
牛客企业服务