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

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

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


题目:

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

思路:

1、找到每个6/7级用户  —— 表user_info
2、找到用户所有活跃的时间(包括试卷和练习),想到用group by + count,但是前提必须是试卷和练习时间在一列里,想到用union ——表a
3、统计总活跃月份数、2021年活跃天数、2021年试卷作答活跃天数、2021年答题活跃天数这四列肯定是要用到date_format()函数,所以可以提前将活跃年、活跃月份、活跃天找出来 —— 表b
4、在表b的结果中,用group by + count(加条件)查询
注意:一开始在表a中用的是uid in (level67),这样的话1003用户就被忽略了,他没有作答,所以在a中必须使用左连接,将ui表作为主表

答案:

with a as (
  select
    uid,
    exam_id as tid,
    submit_time,
    'er' as title
  from
    (
      select
        uid
      from
        user_info
      where
        level in (6, 7)
    ) ssu left join exam_record using(uid)
  union
  select
    uid,
    question_id as tid,
    submit_time,
    'pr' as title
  from
    (
      select
        uid
      from
        user_info
      where
        level in (6, 7)
    ) ssu left join practice_record using(uid)
    )

select uid
    ,count(distinct act_month) as act_month_total
    ,count(distinct (case when act_year =2021 then act_day end)) as act_days_2021
    ,count(distinct (case when act_year =2021 and title = 'er' then act_day end)) as act_days_2021_exam
    ,count(distinct (case when act_year =2021 and title = 'pr' then act_day end)) as act_days_2021_question
from 
(
select
  *,
  year(submit_time) as act_year,
  date_format(a.submit_time, '%Y%m') as act_month,
  date_format(a.submit_time, '%Y%m%d') as act_day
from
  a 
) b
group by uid
order by act_month_total desc,act_days_2021 desc


全部评论
太厉害了学姐~~~
点赞 回复 分享
发布于 2023-08-29 10:46 北京

相关推荐

Southyeung:我说一下我的看法(有冒犯实属抱歉):(1)简历不太美观,给我一种看都不想看的感觉,感觉字体还是排版问题;(2)numpy就一个基础包,机器学习算法是什么鬼?我感觉你把svm那些写上去都要好一点。(2)课程不要写,没人看,换成获奖经历;(3)项目太少了,至少2-3个,是在不行把网上学习的也写上去。
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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