利用上一题中的代码
考试分数(五)
https://www.nowcoder.com/practice/b626ff9e2ad04789954c2132c74c0512
SELECT id, t1.job, score, t_rank FROM( SELECT *, DENSE_RANK() OVER(PARTITION BY job ORDER BY score DESC) AS t_rank FROM grade )t1 JOIN( SELECT job, FLOOR((COUNT(*) + 1) / 2) AS median1, FLOOR((COUNT(*) + 2) / 2) AS median2 FROM grade GROUP BY job )t2 ON t1.job = t2.job WHERE median1 = t_rank OR median2 = t_rank ORDER BY id
使用窗口函数求得score的排序,利用上一题中求中位数位置的方法,筛选出排序等于中位数位置的记录。