题解 | 考试分数(四)

考试分数(四)

https://www.nowcoder.com/practice/502fb6e2b1ad4e56aa2e0dd90c6edf3c

-- 方法1
with a as(
select
    *,
    row_number() over(partition by job order by score desc,id desc) o1,
    row_number() over(partition by job order by score,id) o2,
    count(id) over(partition by job) cnt
from
    grade
)
select
    job,
    o1 start,
    o2 'end'
from a
where cnt%2 <> 0 and o1 = o2

union all

select 
    job,
    o1 start,
    o2 'end'
from a 
where cnt%2 = 0 and cast(o1 as signed) -cast(o2 as signed) = -1
order by job
-- 方法2
select
    job,
    o1 start,
    o2 'end'
from(
    select
        *,
        cast(row_number() over(partition by job order by score desc,id desc) as signed) o1,
        cast(row_number() over(partition by job order by score,id) as signed) o2,
        count(id) over(partition by job) cnt
    from
        grade
) t1
where o1 = o2 or o1-o2 = -1
order by job
-- 方法3
select
    job,
    floor((count(score)+1)/2) start,
    floor((count(score)+2)/2) end 
from grade 
group by job 
order by job

全部评论

相关推荐

10-14 21:00
门头沟学院 Java
吃花椒的狸猫:这个人说的倒是实话,特别是小公司,一个实习生哪里来的那么多要求
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务