题解 | #考试分数(五)#

考试分数(五)

http://www.nowcoder.com/practice/b626ff9e2ad04789954c2132c74c0512

select id,job,score,rk as t_rank
from
(
    select id,g.job,score,start,end,row_number() over(partition by g.job order by score desc) rk
    from grade g 
    left join
    (select job,if(count(*)%2=1,round(count(*)/2,0),round(count(*)/2,0)) as start,if(count(*)%2=1,round(count(*)/2,0),round(count(*)/2+1,0)) as end
    from grade
    group by job
    order by job) j 
    on g.job=j.job
) b
where start=rk or end=rk
order by id

思路很简单,中位数的寻找上一题已经写过了,所以可以直接复制过来

select job,if(count(*)%2=1,round(count(*)/2,0),round(count(*)/2,0)) as start,if(count(*)%2=1,round(count(*)/2,0),round(count(*)/2+1,0)) as end
from grade
group by job
order by job

然后是排名问题,用窗口函数

select id,g.job,score,start,end,row_number() over(partition by g.job order by score desc) rk
from grade g 

然后两表连接一下
图片说明
最后where筛选一下就完成了

全部评论

相关推荐

代码飞升:别用口语,后端就写后端,前端就写前端,最后别光后悔
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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