中位数——首先要得到组内的排序和总的记录数量,然后根据中位数的特点构造筛选条件:t/2 <= rk and rk <= t/2 + 1, 例:奇数记录排序后1,2,3 中位数是2,其满足2>= 3/2(1.5), 2<= 3/2+1(2.5);偶数记录排序后1,2 中位数是1和2,满足1/2 = 0.5 ,1/2 + 1 = 1.5 和 2/2 =1, 2/2 + 1 = 2,即 0.5 <= 1 <= 1.5,
1 <= 2 <= 2。t/2 <= rk and rk <= t/2 + 1均可适用奇偶情况。
select a.id, a.job, a.score, a.rk from (select *, row_number() over(partition by job order by score desc) as rk, count(*) over(partition by job) as t from grade) as a where t/2 <= rk and rk <= t/2 + 1 order by id asc;