题解 | #每个6/7级用户活跃情况#
每个6/7级用户活跃情况
https://www.nowcoder.com/practice/a32c7c8590324c96950417c57fa6ecd1
with tmp as
(
(select
uid,
'exam' as type,
date_format(start_time, '%Y%m') as act_month,
date_format(start_time, '%Y%m%d') as act_day
from exam_record
where uid in
(select
uid
from user_info
where level in (6, 7)
)
)
union all
(select
uid,
'practice' as type,
date_format(submit_time, '%Y%m') as act_month,
date_format(submit_time, '%Y%m%d') as act_day
from practice_record
where uid in
(select
uid
from user_info
where level in (6, 7)
)
)
)
select
a.uid
,count(distinct act_month) as act_month_total
,count(distinct if(year(act_day) = 2021, act_day, null)) as act_days_2021
,count(distinct if(type = 'exam' and year(act_day) = 2021, act_day, null)) as act_days_2021_exam
,count(distinct if(type = 'practice' and year(act_day) = 2021, act_day, null)) as act_days_2021_question
from tmp
right join
(select
uid
from user_info
where level in (6, 7)
) a -- 防止活跃数均为0的被忽略
on a.uid = tmp.uid
group by a.uid
order by act_month_total desc, act_days_2021 desc
;