题解 | #最差是第几名(二)#
https://www.nowcoder.com/practice/165d88474d434597bcd2af8bf72b24f1
构造 最小和最大排名区间 的简化方法
看到有小伙伴用了最大和最小排名,但是用的的union连接,可以将union简化为between(考虑了奇偶)
select grade from (select grade,number,total,max_rank-number+1 as min_rank,max_rank from (select grade,number,sum(number) over(order by grade) max_rank, sum(number)over() as 'total' from class_grade )a1 )a2 where floor((total+1)/2) between min_rank and max_rank or floor((total+2)/2) between min_rank and max_rank