题解 | #每个6/7级用户活跃情况#

每个6/7级用户活跃情况

https://www.nowcoder.com/practice/a32c7c8590324c96950417c57fa6ecd1

发现大家的做法好简单,我的做法好复杂。。。。。
方法一:复杂
select b5.uid,ifnull(act_month_total,0),ifnull(act_days_2021,0)
,ifnull(act_days_2021_exam,0),ifnull(act_days_2021_question,0)
from 
(select uid
from user_info
where level in(6,7)) as b5
left join
###6/7级用户总活跃月份数以及2021年活跃天数
(select uid,count(distinct uid,date_format(date(submit_time),'%Y%m')) as act_month_total,
count(distinct uid,(case when year(submit_time)='2021' 
                    then date(submit_time) end) ) as act_days_2021
from
  (select uid,submit_time
   from exam_record
   union all
   select uid,submit_time
   from practice_record) as b1 
group by uid) as b2
on b5.uid=b2.uid
left join
###2021年试卷作答活跃天数
(select uid,count(distinct uid,date(submit_time)) as act_days_2021_exam
from exam_record
where year(submit_time)='2021'
group by uid) as b3
on b5.uid=b3.uid
left join
###2021年答题活跃天数
(select uid,count(distinct uid,date(submit_time)) as act_days_2021_question
from practice_record
where year(submit_time)='2021'
group by uid) as b4
on b5.uid=b4.uid
order by act_month_total desc,act_days_2021 desc
方法二:简单
select b3.uid,ifnull(act_month_total,0) ,ifnull(act_days_2021,0)
,ifnull(act_days_2021_exam,0),ifnull(act_days_2021_question,0)
from
(select uid
from user_info
where level in(6,7)) as b3
left join
(select uid,count(distinct uid,date_format(submit_time,'%Y%m')) act_month_total
,count(distinct uid,date(case when year(submit_time)='2021' 
                         then submit_time end)) act_days_2021
,count(distinct uid,(case when tag=1 and year(submit_time)='2021' 
                     then date(submit_time) end) ) as act_days_2021_exam
,count(distinct uid,(case when tag=2 and year(submit_time)='2021' 
                     then date(submit_time) end) ) as act_days_2021_question
from
(select uid,submit_time,1 tag
 from exam_record
 union all
 select uid,submit_time,2 tag
 from practice_record) as b1
 group by uid) as b2
 on b3.uid=b2.uid
 order by act_month_total desc,act_days_2021 desc
 


全部评论

相关推荐

04-08 23:37
已编辑
东华大学 结构工程师
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务