题解 | #每月及截止当月的答题情况#
每月及截止当月的答题情况
https://www.nowcoder.com/practice/1ce93d5cec5c4243930fc5e8efaaca1e
select *, max(month_add_uv) over (order by mon) as max_month_add_uv, sum(month_add_uv) over (order by mon) cum_sum_uv from ( select date_format(start_time,'%Y%m') mon, count(distinct uid) mau, count(distinct case when ranking=1 then uid end) month_add_uv from ( select *,row_number() over (partition by uid order by start_time ) ranking from exam_record ) b group by date_format(start_time,'%Y%m') ) c
请输出自从有用户作答记录以来,每月的试卷作答记录中月活用户数、新增用户数、截止当月的单月最大新增用户数、截止当月的累积用户数。
- 月活用户数:count ( uid) group by mon
- 新增用户数
- 麻烦的点在于计算新增用户数,使用row_number() over (partition by uid order by mon ) ranking,对每个用户的登录时间排序,ranking为1表示第一次登录
- 截止当月的单月最大新增用户数:针对新增用户数,使用窗口函数max()
- 截至当月累计的用户数:针对新增用户数,使用窗口函数sum()