相比前一题,虽然都是困难,但这一题简单多了。

select tag,exam_cnt_20,exam_cnt_21,concat(round(100*(exam_cnt_21-exam_cnt_20)/exam_cnt_20,1),'%') growth_rate,
exam_cnt_rank_20,
exam_cnt_rank_21,
cast(exam_cnt_rank_21 as signed)-cast(exam_cnt_rank_20 as signed)  rank_delta
from (
select 
e1.tag,
count(distinct if(e2.submit_time is not null,e2.id,null))  exam_cnt_20,
count(distinct if(e3.submit_time is not null,e3.id,null))  exam_cnt_21,
rank()over(order by count(distinct if(e2.submit_time is not null,e2.id,null)) desc ) exam_cnt_rank_20,
rank()over(order by count(distinct if(e3.submit_time is not null,e3.id,null)) desc ) exam_cnt_rank_21
from examination_info e1 
left join exam_record e2 on e1.exam_id=e2.exam_id and  year(e2.start_time) =2020  and month(e2.start_time)<=6 
left join exam_record e3 on e1.exam_id=e3.exam_id and  year(e3.start_time) =2021  and month(e3.start_time)<=6  
group by e1.tag) a
where exam_cnt_20 >0 and exam_cnt_21>0
order by growth_rate desc,exam_cnt_rank_21 desc