牛客SQL135——每个6/7级用户活跃情况

题目描述
https://www.nowcoder.com/practice/a32c7c8590324c96950417c57fa6ecd1

思路: 请统计每个6/7级用户总活跃月份数、2021年活跃天数、2021年试卷作答活跃天数、2021年答题活跃天数,按照总活跃月份数、2021年活跃天数降序排序。
本题的难度在于要查找的东西太多了,很难一步写出来,可以先根据每个要求写出对应的sql最后在连接到一个大表里即可

1.先找到6/7级用户

select uid from user_info where level=6 or level=7

2.总活跃月份数

select u.uid,count(distinct date_format(submit_time,"%Y%m")) as act_month_total
from user_info as u left join (
     select uid,submit_time from exam_record
     union all 
     select uid,submit_time from practice_record  
 ) as a on u.uid=a.uid
where u.uid in (select uid from user_info where level=6 or level=7)
group by u.uid
order by act_month_total desc

3.2021年活跃天数

select u.uid,count(distinct if(year(date_format(submit_time,"%Y%m%d"))=2021,(date_format(submit_time,"%Y%m%d")),null)) as act_days_2021
from user_info as u left join(
    select uid,submit_time from exam_record
    union all 
    select uid,submit_time from practice_record  
) as a on u.uid=a.uid
where u.uid in (select uid from user_info where level=6 or level=7) 
group by u.uid
order by act_days_2021 desc

4.2021年试卷作答活跃天数

select u.uid,count(distinct if(year(date_format(submit_time,"%Y%m%d"))=2021,(date_format(submit_time,"%Y%m%d")),null)) as act_days_2021_exam
from user_info as u left join exam_record as r on u.uid=r.uid
where u.uid in (select uid from user_info where level=6 or level=7)
group by u.uid


5.2021年答题活跃天数

select u.uid,count(distinct if(year(date_format(submit_time,"%Y%m%d"))=2021,(date_format(submit_time,"%Y%m%d")),null)) as act_days_2021_question
from user_info as u left join practice_record as p on u.uid=p.uid
where u.uid in (select uid from user_info where level=6 or level=7) 
group by u.uid

代码合并,使用left join进行连接,但是因为我们在每一个小块里面已经是left join了所以最后大表链接的时候也可以直接用join

# 请统计每个6/7级用户总活跃月份数、2021年活跃天数、2021年试卷作答活跃天数、2021年答题活跃天数,
# 按照总活跃月份数、2021年活跃天数降序排序。


select u.uid,one.act_month_total,two.act_days_2021,three.act_days_2021_exam,four.act_days_2021_question
from user_info as u join(
    select u.uid,count(distinct date_format(submit_time,"%Y%m")) as act_month_total
    from user_info as u left join (
    select uid,submit_time from exam_record
    union all 
    select uid,submit_time from practice_record  
    ) as a on u.uid=a.uid
    where u.uid in (select uid from user_info where level=6 or level=7)
    group by u.uid
) as one on u.uid=one.uid
left join(
    select u.uid,count(distinct if(year(date_format(submit_time,"%Y%m%d"))=2021,(date_format(submit_time,"%Y%m%d")),null)) as act_days_2021
    from user_info as u left join(
    select uid,submit_time from exam_record
    union all 
    select uid,submit_time from practice_record  
    ) as a on u.uid=a.uid
    where u.uid in (select uid from user_info where level=6 or level=7) 
    group by u.uid
)as two on u.uid=two.uid
left join(
    select u.uid,count(distinct if(year(date_format(submit_time,"%Y%m%d"))=2021,(date_format(submit_time,"%Y%m%d")),null)) as act_days_2021_exam
    from user_info as u left join exam_record as r on u.uid=r.uid
    where u.uid in (select uid from user_info where level=6 or level=7)
    group by u.uid
)as three on u.uid=three.uid
left join(
    select u.uid,count(distinct if(year(date_format(submit_time,"%Y%m%d"))=2021,(date_format(submit_time,"%Y%m%d")),null)) as act_days_2021_question
    from user_info as u left join practice_record as p on u.uid=p.uid
    where u.uid in (select uid from user_info where level=6 or level=7) 
    group by u.uid
)as four on u.uid=four.uid
order by one.act_month_total desc,two.act_days_2021 desc
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值