题解 | #每个6/7级用户活跃情况#月份和日期区分开,没有记录显示0所以用左连接
每个6/7级用户活跃情况
http://www.nowcoder.com/practice/a32c7c8590324c96950417c57fa6ecd1
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