参考考试分数(四),稍微改动

考试分数(五)

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

  1. 在原表上新添加一列,使用窗口函数排序(观察输出结果,使用逆序desc)

    select *,row_number() over(partition by job order by score desc) as t_rank
    from grade
  2. 参考考试分数(四)的答案,取其中中位数的位置

    select job,round(count(id)/2) as 'start',round((count(id)+1)/2) as 'end'
    from grade 
    group by job 
    order by job
  3. 连接两表,最终结果

    select a.id,a.job,a.score,a.t_rank
    from (
     select *,row_number() over(partition by job order by score desc) as t_rank
     from grade)a
    inner join (
     select job,round(count(id)/2) as 'start',round((count(id)+1)/2) as 'end'
     from grade 
     group by job 
     order by job)b
    on a.job = b.job
    where a.t_rank = b.start
    or a.t_rank = b.end
    order by id
全部评论

相关推荐

牛客5655:其他公司的面试(事)吗
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务