分步骤进行:1、先把未完成率前百分之50的人找出来,用到ntile()over()开窗函数
2、再把每个用户近三月的完成情况查出来
3、根据前两步的结果进行关联,选择符合条件的用户即可
select t1.uid,start_month,total_cnt,complete_cnt from user_info t1 join ( select uid,date_format(start_time,'%Y%m') as start_month, count(1) as total_cnt,count(submit_time) as complete_cnt from ( select uid,start_time,submit_time, dense_rank()over(partition by uid order by date_format(start_time,'%Y%m') desc) as rn from exam_record ) t where rn <= 3 group by uid,date_format(start_time,'%Y%m') ) t2 on t1.uid = t2.uid where level in (6,7) and t1.uid in ( select uid from ( select uid,incomplete_rate, ntile(2)over(order by incomplete_rate desc) as rn from( select uid,sum(if(submit_time is null,1,0))/count(1) as incomplete_rate from examination_info t1 join exam_record t2 on t1.exam_id = t2.exam_id where tag = 'SQL' group by uid )t ) tt where rn = 1 ) order by uid,start_month