明确题意:
计算2021年每个月里试卷作答区用户平均月活跃天数avg_active_days和月度活跃人数mau
问题分解:
- 筛选2021年活跃的用户作答记录:where submit_time is not NULL and YEAR(submit_time)='2021'
- 获取用户-活跃日期的去重结果:select distinct uid, DATE_FORMAT(submit_time, "%Y%m%d") as ymd
- 统计每个月的平均活跃天数和月活人数:
- 按月份分组:提取月份SUBSTR(ymd, 1, 6) as
month
;分组group bymonth
- 统计月活人数:count(distinct uid) as mau
- 统计平均活跃天数:总活跃人天数除以活跃人数count(1) / count(distinct uid)
- 按月份分组:提取月份SUBSTR(ymd, 1, 6) as
细节问题:
- 表头重命名:as
- 平均活跃天数保留2位小数:round(..., 2)
完整代码:
select SUBSTR(ymd, 1, 6) as `month`,
round(count(1) / count(distinct uid), 2) as avg_active_days,
count(distinct uid) as mau
from (
select distinct uid, DATE_FORMAT(submit_time, "%Y%m%d") as ymd
from exam_record
where submit_time is not NULL and YEAR(submit_time)='2021'
) as t_active_day
group by `month`