select * from ( select tag as tid,uid,row_number()over(partition by tag order by max_score desc,min_score desc,uid desc) as ranking from ( select uid,max(score) as max_score,min(score) as min_score,tag from exam_record t1 join examination_info t3 on t1.exam_id = t3.exam_id group by uid,tag ) t ) t2 where ranking < 4