题解 | #最差是第几名(二)#
最差是第几名(二)
http://www.nowcoder.com/practice/165d88474d434597bcd2af8bf72b24f1
知识点
- 先计算出总共有多少名,使用窗口按照grade正序排序求和为一列,同理使用按照grade逆序求和
- 中位数就是正序列>= 总共/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