select uid,a1 exam_complete_cnt from( select uid,count(start_time) a1,count(submit_time) a2 from( select uid,exam_id,start_time,submit_time,dense_rank() over(partition by uid order by date_format(start_time, '%Y%m') desc) rankk from exam_record) t1 where rankk <= 3 group by uid having a1 = a2) t2 order by exam_complete_cnt desc, uid desc
要点:where的执行在窗口函数之前