参考考试分数(四),稍微改动
考试分数(五)
http://www.nowcoder.com/practice/b626ff9e2ad04789954c2132c74c0512
在原表上新添加一列,使用窗口函数排序(观察输出结果,使用逆序desc)
select *,row_number() over(partition by job order by score desc) as t_rank from grade
参考考试分数(四)的答案,取其中中位数的位置
select job,round(count(id)/2) as 'start',round((count(id)+1)/2) as 'end' from grade group by job order by job
连接两表,最终结果
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