小笨蛋看过来| #最差是第几名(二)#

最差是第几名(二)

https://www.nowcoder.com/practice/165d88474d434597bcd2af8bf72b24f1

select
    grade,
    number,
    sum(number) over(order by grade asc) max_rank
from
    class_grade
order by 
    grade asc

select 
    a.grade,
    a.number,
    a.max_rank-a.number+1 min_rank,
    max_rank
from
    (select grade, number, sum(number) over(order by grade) max_rank from class_grade) a

select
    (case when sum(number) % 2 = 0 then round(sum(number)/2 ,0) else round((sum(number)+1)/2 ,0) end) as mid_number
from class_grade 
union  # 注意用union 而不是 union all 这样会帮忙去重,即当sum(number)是奇数时,可以只输出一个
select
    (case when sum(number) % 2 = 0 then round((sum(number)+2)/2 ,0) else round((sum(number)+1)/2 ,0) end) as mid_number
from class_grade 

select 
    distinct b.grade
from
(
    select 
        a.grade,
        a.number,
        a.max_rank-a.number+1 min_rank,
        max_rank
    from
        (select grade, number, sum(number) over(order by grade) max_rank from class_grade) a
) b
join 
(
    select
        (case when sum(number) % 2 = 0 then round(sum(number)/2 ,0) else round((sum(number)+1)/2 ,0) end) as mid_grade
    from class_grade 
    union  # 注意用union 而不是 union all 这样会帮忙去重,即当sum(number)是奇数时,可以只输出一个
    select
        (case when sum(number) % 2 = 0 then round((sum(number)+2)/2 ,0) else round((sum(number)+1)/2 ,0) end) as mid_grade
    from class_grade 
) c
on c.mid_grade between b.min_rank and b.max_rank
order by grade asc

全部评论

相关推荐

10-30 23:23
已编辑
中山大学 Web前端
去B座二楼砸水泥地:这无论是个人素质还是专业素质都👇拉满了吧
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
11-26 16:06
已编辑
快手电商 后端 23k-35k
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务