题解 | #每类试卷得分前3名#
每类试卷得分前3名
https://www.nowcoder.com/practice/255aa1863fe14aa88694c09ebbc1dbca
SELECT * FROM (SELECT a.tag tid, b.uid uid, ROW_NUMBER() OVER(PARTITION BY a.tag ORDER BY MAX(b.score) DESC, MIN(b.score) DESC, MAX(b.uid) DESC) ranking FROM examination_info a JOIN exam_record b USING(exam_id ) GROUP BY tid, uid) T HAVING ranking <= 3 # WHERE ranking <= 3
ROW_NUMBER() 唯一排名 1,2,3
RANK() 重复且跳过 1,1,3,3,5
DENSE_RANK() 重复但不跳过 1,1,2,2,2,3
题目要求找到每类试卷得分的前三名,排序条件是先根据最大得分,再根据最小得分、用户id
所以使用ROW_NUMBER(),再把这个排序条件显式化
前三名,就筛选出ranking <=3的,不知道为啥HAVING 也对,我是想直接一个查询解决问题的,但是报错