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

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

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

WITH t as(
SELECT uid,submit_time
from exam_record
UNION
SELECT uid,submit_time
from practice_record
),
t_level as(
SELECT uid
FROM user_info
WHERE level = 6 OR level = 7
),
t_act_month_total as(
SELECT uid,
COUNT(DISTINCT DATE_FORMAT(submit_time,'%Y%m')) AS act_month_total  
FROM t
GROUP BY uid  
),
t_act_days_2021 as(
SELECT uid,
COUNT(DISTINCT DATE_FORMAT(submit_time,'%Y%m%d')) AS act_days_2021  
FROM t  
WHERE YEAR(submit_time) = 2021
GROUP BY uid
),
t_act_days_2021_exam as(
SELECT uid,
COUNT(DISTINCT DATE(submit_time)) AS act_days_2021_exam
FROM exam_record
WHERE YEAR(submit_time) = 2021
GROUP BY uid
),
t_act_days_2021_question as(
SELECT uid,
COUNT(DISTINCT DATE(submit_time)) AS act_days_2021_question
FROM practice_record
WHERE YEAR(submit_time) = 2021
GROUP BY uid
)

SELECT t_level.uid,
IF(act_month_total IS NULL,0,act_month_total),
IF(act_days_2021 IS NULL,0,act_days_2021),
IF(act_days_2021_exam IS NULL,0,act_days_2021_exam),	
IF(act_days_2021_question IS NULL,0,act_days_2021_question)
FROM t_level
LEFT OUTER JOIN t_act_month_total ON t_level.uid = t_act_month_total.uid
LEFT OUTER JOIN t_act_days_2021 ON t_act_month_total.uid = t_act_days_2021.uid
LEFT OUTER JOIN t_act_days_2021_exam ON t_act_days_2021.uid = t_act_days_2021_exam.uid
LEFT OUTER JOIN t_act_days_2021_question ON t_act_days_2021_question.uid = t_act_days_2021_exam.uid
ORDER BY act_month_total DESC,act_days_2021 DESC;

全部评论

相关推荐

11-02 09:49
已编辑
货拉拉_测试(实习员工)
热爱生活的仰泳鲈鱼求你们别卷了:没事楼主,有反转查看图片
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务