知识点

  1. 先计算出总共有多少名,使用窗口按照grade正序排序求和为一列,同理使用按照grade逆序求和
  2. 中位数就是正序列>= 总共/2,以及逆序列>=总共/2,两者之间的交集就是中位数

代码

select grade 
from (select grade,
             (select sum(number) 
             from class_grade) as total,
             sum(number) over(order by grade) as a,
             sum(number) over(order by grade desc) as b
      from class_grade
     ) as t1
where a >= total/2 and b >=total/2
order by grade