题解 | #每类试卷得分前3名#
每类试卷得分前3名
https://www.nowcoder.com/practice/255aa1863fe14aa88694c09ebbc1dbca
本题考查要点包括:多表连接、聚合查询、窗口函数、排序函数
思路步骤:先使用上述各类函数查询出所需的字段包括排序字段,之后再对排名进行筛选即可。
select // ⑥ 查询最终所需字段 tag, uid, ranking from ( select // ③ 查询得到tag、uid、ranking等所需字段 tag, uid, row_number() over( // 排序函数包括rank()over()、dense_rank()over()、row_number()over()等,根据题意此处使用row_number()over() partition by tag order by max(score) desc, min(score) desc, uid desc ) ranking from exam_record t1 left join examination_info t2 on t1.exam_id = t2.exam_id // ① 窗口函数的数据源:将exam_record表与examination_info表按考试id连接 group by tag,uid // ② 按照题目类型及用户id进行区域划分 ) t // ④ 将查询所得信息生成新表作为最终数据源 where ranking < 4 // ⑤ 对排名进行筛选,保留每个题型分数在前三名的数据注:关于几类常用rank函数的区分,可参考: