select t.uid,count(distinct month) as act_month_total ,count(distinct if(year(day) = 2021,day,null)) as act_days_2021 ,count(distinct if(year(day) = 2021 and flag = 'exam',day,null)) as act_days_2021_exam ,count(distinct if(year(day) = 2021 and flag = 'practice',day,null)) as act_days_2021_question from ( select t1.uid,t2.month,t2.day,t2.flag from (select uid from user_info where level in (6,7)) t1 left join( select uid,date_format(start_time,'%Y%m') as month, date_format(start_time,'%Y%m%d') as day, 'exam' as flag from exam_record union select uid,date_format(submit_time,'%Y%m') as month, date_format(submit_time,'%Y%m%d') as day, 'practice' as flag from practice_record ) t2 on t1.uid = t2.uid ) t group by uid order by act_month_total desc,act_days_2021 desc