嵌套了两次,首先根据tag和uid分组并找出每组最大值最小值,然后根据最大值最小值以及uid使用row_number赋予排序标签,最后限制前三名
select * from
(select tag, uid, row_number() over (partition by tag order by max_grade desc, min_grade desc, uid desc) as ranking
from
(select tag, uid, max(score) as max_grade, min(score) as min_grade
from examination_info join exam_record using(exam_id)
group by tag, uid) as temp) as temp2
where ranking < 4