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

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

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

SELECT
  uid,
  COUNT(DISTINCT DATE_FORMAT(tb.answer_time, '%Y%m')) act_month_total,
  COUNT(
    DISTINCT IF(
      YEAR(tb.answer_time) = 2021,
      DATE_FORMAT(tb.answer_time, '%Y%m%d'),
      NULL
    )
  ) act_days_2021,
  COUNT(
    DISTINCT IF(
      YEAR(tb.answer_time) = 2021
      AND tag = 'exam',
      DATE_FORMAT(tb.answer_time, '%Y%m%d'),
      NULL
    )
  ) act_days_2021_exam,
  COUNT(
    DISTINCT IF(
      YEAR(tb.answer_time) = 2021
      AND tag = 'practice',
      DATE_FORMAT(tb.answer_time, '%Y%m%d'),
      NULL
    )
  ) act_days_2021_question
FROM
  user_info ui
  LEFT JOIN (
    SELECT
      uid,
      start_time answer_time,
      'exam' tag
    FROM
      exam_record
    UNION
    SELECT
      uid,
      submit_time answer_time,
      'practice' tag
    FROM
      practice_record
  ) tb USING(uid)
WHERE
  ui.level IN (6, 7)
GROUP BY
  uid
ORDER BY
  act_month_total DESC,
  act_days_2021 DESC;

全部评论
活跃天数为什么只对日期去重?为什么不像17题一样,对 uid和date_format同时去重:round((count distinct uid, date_format(submit_time, '%y%m%d')) / count(distinct uid), 2)
点赞 回复 分享
发布于 2022-06-22 09:31

相关推荐

06-13 10:15
门头沟学院 Java
想去夏威夷的大西瓜在...:我也是27届,但是我现在研一下了啥项目都没有呀咋办,哎,简历不知道咋写
点赞 评论 收藏
分享
写不来代码的小黑:这么小的城市能有做it的公司也不容易
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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