1.首先用一个子查询选出需要的字段并用row_number根据job分组标记排序,然后用另一个子查询根据job分组统计每组总量,然后连接两表。用floor和ceil从总量算出中位数并限制排序值为中位数,最后根据id排序。这里用两个单独的子查询来获取排序编号和每组数量是因为我忘记count()也可以开窗口函数了。。。。实际上第二个子查询完全没必要,所以我在第二版里把第二个子查询去掉了

select temp.id, temp.job, temp.score, temp.t_rank from
(select g.id, g.job, g.score, 
row_number() over (partition by g.job order by g.score desc) as t_rank
from grade as g) as temp,
(select job, count(id) as cnt from grade
group by job) as temp2
where temp.job = temp2.job and
(temp.t_rank = floor((temp2.cnt+1)/2) or temp.t_rank = ceil((temp2.cnt+1)/2))
order by temp.id

2.把上一个方法两个子查询合并了

select temp.id, temp.job, temp.score, temp.t_rank from
(select id, job, score, 
row_number() over (partition by job order by score desc) as t_rank,
count(id) over (partition by job) as cnt
from grade) as temp
where temp.t_rank = floor((temp.cnt+1)/2) or temp.t_rank = ceil((temp.cnt+1)/2)
order by temp.id