描述
现有用户信息表user_info(uid用户ID,nick_name昵称, achievement成就值, level等级, job职业方向, register_time注册时间):
id | uid | nick_name | achievement | level | job | register_time |
1 | 1001 | 牛客1号 | 3100 | 7 | 算法 | 2020-01-01 10:00:00 |
2 | 1002 | 牛客2号 | 2300 | 7 | 算法 | 2020-01-01 10:00:00 |
3 | 1003 | 牛客3号 | 2500 | 7 | 算法 | 2020-01-01 10:00:00 |
4 | 1004 | 牛客4号 | 1200 | 5 | 算法 | 2020-01-01 10:00:00 |
5 | 1005 | 牛客5号 | 1600 | 6 | C++ | 2020-01-01 10:00:00 |
6 | 1006 | 牛客6号 | 2600 | 7 | C++ | 2020-01-01 10:00:00 |
试卷信息表examination_info(exam_id试卷ID, tag试卷类别, difficulty试卷难度, duration考试时长, release_time发布时间):
id | exam_id | tag | difficulty | duration | release_time |
1 | 9001 | SQL | hard | 60 | 2021-09-01 06:00:00 |
2 | 9002 | C++ | easy | 60 | 2021-09-01 06:00:00 |
3 | 9003 | 算法 | medium | 80 | 2021-09-01 10:00:00 |
试卷作答记录表exam_record(uid用户ID, exam_id试卷ID, start_time开始作答时间, submit_time交卷时间, score得分):
uid | exam_id | start_time | submit_time | score |
1001 | 9001 | 2021-09-01 09:01:01 | 2021-09-01 09:31:00 | 78 |
1001 | 9001 | 2021-09-01 09:01:01 | 2021-09-01 09:31:00 | 81 |
1005 | 9001 | 2021-09-01 19:01:01 | 2021-09-01 19:30:01 | 85 |
1005 | 9002 | 2021-09-01 12:01:01 | 2021-09-01 12:31:02 | 85 |
1006 | 9003 | 2021-09-07 10:01:01 | 2021-09-07 10:21:59 | 84 |
1006 | 9001 | 2021-09-07 10:01:01 | 2021-09-07 10:21:01 | 81 |
1002 | 9001 | 2020-09-01 13:01:01 | 2020-09-01 13:41:01 | 81 |
1005 | 9001 | 2021-09-01 14:01:01 | (NULL) | (NULL) |
题目练习记录表practice_record(uid用户ID, question_id题目ID, submit_time提交时间, score得分):
uid | question_id | submit_time | score |
1001 | 8001 | 2021-08-02 11:41:01 | 60 |
1004 | 8001 | 2021-08-02 19:38:01 | 70 |
1004 | 8002 | 2021-08-02 19:48:01 | 90 |
1001 | 8002 | 2021-08-02 19:38:01 | 70 |
1004 | 8002 | 2021-08-02 19:48:01 | 90 |
1006 | 8002 | 2021-08-04 19:58:01 | 94 |
1006 | 8003 | 2021-08-03 19:38:01 | 70 |
1006 | 8003 | 2021-08-02 19:48:01 | 90 |
1006 | 8003 | 2020-08-01 19:38:01 | 80 |
请统计每个6/7级用户总活跃月份数、2021年活跃天数、2021年试卷作答活跃天数、2021年答题活跃天数,按照总活跃月份数、2021年活跃天数降序排序。由示例数据结果输出如下:
uid | act_month_total | act_days_2021 | act_days_2021_exam | act_days_2021_question |
1006 | 3 | 4 | 1 | 3 |
1001 | 2 | 2 | 1 | 1 |
1005 | 1 | 1 | 1 | 0 |
1002 | 1 | 0 | 0 | 0 |
1003 | 0 | 0 | 0 | 0 |
解释:6/7级用户共有5个,其中1006在202109、202108、202008共3个月活跃过,2021年活跃的日期有20210907、20210804、20210803、20210802共4天,2021年在试卷作答区20210907活跃1天,在题目练习区活跃了3天。
代码1:
with cte1 as(
select uid
from
user_info
where level=6 or level=7
),
cte2 as(
select uid,start_time as act_time,'exam' as label from exam_record
union
select uid,submit_time as act_time,'question' as lebel from practice_record
),
cte3 as(
select uid,
count(distinct left(act_time,7)) as act_month_total
from cte1 left join cte2 using(uid)
group by uid
order by act_month_total desc
),
cte4 as(
select uid,
count(distinct left(act_time,10)) as act_days_2021
from cte1 left join cte2 using(uid)
where left(act_time,4)=2021
group by uid
),
cte5 as(
select uid,
count(distinct left(act_time,10)) as act_days_2021_exam
from cte1 left join cte2 using(uid)
where left(act_time,4)=2021 and label='exam'
group by uid
),
cte6 as(
select uid,
count(distinct left(act_time,10)) as act_days_2021_question
from cte1 left join cte2 using(uid)
where left(act_time,4)=2021 and label='question'
group by uid
)
select uid,
ifnull(act_month_total,0),ifnull(act_days_2021,0),
ifnull(act_days_2021_exam,0),ifnull(act_days_2021_question,0)
from cte1 left join cte3 using(uid)
left join cte4 using(uid)
left join cte5 using(uid)
left join cte6 using(uid)
order by act_month_total desc,act_days_2021 desc
代码2(来源于讨论区的机智写法):
SELECT uid,
COUNT(DISTINCT DATE_FORMAT(act_time,'%Y%m')) as act_month_total,
COUNT(DISTINCT IF(YEAR(act_time)=2021,act_time,null)) as act_days_2021,
COUNT(DISTINCT IF(YEAR(act_time)=2021 and tag='exam',act_time,null)) as act_days_2021_exam,
COUNT(DISTINCT IF(YEAR(act_time)=2021 and tag='question',act_time,null)) as act_days_2021_question
FROM user_info
LEFT JOIN (
SELECT uid,DATE(start_time) as act_time,'exam' as tag
FROM exam_record
UNION
SELECT uid,DATE(submit_time) as act_time,'question' as tag
FROM practice_record
)t
using(uid)
WHERE level>=6
GROUP BY uid
ORDER BY act_month_total DESC,act_days_2021 DESC;