select * from(select tag, uid, ROW_NUMBER() over (partition by e.tag order by max(r.score)desc,min(score)desc,uid desc) as ranking from examination_info as e #表1 right join exam_record as r #表2 on e.exam_id =r.exam_id group by tag,uid) t where ranking<=3
# 考核知识点:
# rank() dense_rank(),row_number()三种排名函数的使用
rank() 考虑数据的重复性 ,挤占坑位 1,2,2,4
dense_rank() 考虑数据重复性,不挤占坑位 1,2,2,3
row_number() 不考虑数据的重复性,按照顺序依次标上顺序 1,2,3,4
思路:
1,先计算出tag的排名,ROW_NUMBER() over (partition by e.tag order by max(r.score)desc,min(score)desc,uid desc) as ranking ,分组依据为tag,排序依据为'分数的最大值-倒序,分数的最小值-倒序,uid-倒序
2,外层嵌套查询,查询想要的数据,select* from(select.......),因为要获取前三名,所以外层查询使用内层查询的相关字段进行过滤,
where ranking<=3
个人认为最难的部分是:ROW_NUMBER() over (partition by e.tag order by max(r.score)desc,min(score)desc,uid desc) as ranking