select uid, count(start_time) as exam_complete_cnt from ( select *, dense_rank() over(partition by uid order by date_format(start_time, '%Y%m') desc) as ranking from exam_record ) as t where ranking <= 3 group by uid having count(start_time) = count(submit_time) order by exam_complete_cnt desc, uid desc
1.先筛选,用partition by uid将每个用户的信息分组,再按照开始作答时间的年月降序排列
2. ranking <= 3, 表示月份为最近的三个月,新表中为近三个月有作答记录的人的信息
3.再选出全部完成作答的用户的信息,即开始作答时间个数等于提交时间个数,或者用户id出现次数等于其分数的个数
4.再统计用户的试卷作答数,用count(start_time)
5.按完成试卷数和用户名降序排列