select uid,m,t_cnt,com_cnt
from(
select *,
dense_rank() over(order by incom_rate asc,uid) as dra_incom_rate_a,
dense_rank() over(order by incom_rate desc,uid desc) as dra_incom_rate_d,
row_number() over(partition by uid order by m desc) as rn_m
from(
select distinct
a.uid,date_format(a.start_time,'%Y%m') as m,c.level,
1-count(case when b.tag='SQL' then a.score end) over(partition by a.uid)
/count(case when b.tag='SQL' then a.id end) over(partition by a.uid) as incom_rate,
count(a.id) over(partition by 
                 a.uid,date_format(a.start_time,'%Y%m')) as t_cnt,
count(a.score) over(partition by 
                    a.uid,date_format(a.start_time,'%Y%m')) as com_cnt
from exam_record a 
left join examination_info b on a.exam_id=b.exam_id
left join user_info c on a.uid=c.uid)d)e 
where level>=6 
and dra_incom_rate_a>=(dra_incom_rate_a + dra_incom_rate_d)/2
and rn_m<4
order by uid,m