题解 | #每个6/7级用户活跃情况#
每个6/7级用户活跃情况
https://www.nowcoder.com/practice/a32c7c8590324c96950417c57fa6ecd1
select c1.uid,act_month_total,ifnull(act_days_2021,0) act_days_2021,ifnull(act_days_2021_exam,0),ifnull(act_days_2021_question,0) from (select d.uid uid,count(distinct month) act_month_total from (select a.uid uid,date_format(start_time,'%Y%m') month from (select * from user_info where level in (6,7)) a left join exam_record b using(uid) group by uid,date_format(start_time,'%Y%m') union select a.uid uid,date_format(submit_time,'%Y%m') month from (select * from user_info where level in (6,7)) a left join practice_record b using(uid) group by uid,date_format(submit_time,'%Y%m')) d group by uid) c1 left join (select d.uid uid,count(distinct day) act_days_2021 from (select a.uid uid,date_format(start_time,'%Y%m%d') day from (select * from user_info where level in (6,7)) a left join exam_record b using(uid) where year(start_time) = 2021 group by uid,date_format(start_time,'%Y%m%d') union select a.uid uid,date_format(submit_time,'%Y%m%d') day from (select * from user_info where level in (6,7)) a left join practice_record b using(uid) where year(submit_time) = 2021 group by uid,date_format(submit_time,'%Y%m%d')) d group by uid) c2 using(uid) left join (select t1.uid uid,count(distinct day1) act_days_2021_exam,count(distinct day2) act_days_2021_question from (select a.uid uid,date_format(start_time,'%Y%m%d') day1 from (select * from user_info where level in (6,7)) a left join exam_record b using(uid) where year(start_time) = 2021 group by uid,date_format(start_time,'%Y%m%d')) t1 left join (select a.uid uid,date_format(submit_time,'%Y%m%d') day2 from (select * from user_info where level in (6,7)) a left join practice_record b using(uid) where year(submit_time) = 2021 group by uid,date_format(submit_time,'%Y%m%d')) t2 using(uid) group by uid) c3 using(uid) order by act_month_total desc,act_days_2021 desc用了连接、合并