题解 | #每月及截止当月的答题情况#
每月及截止当月的答题情况
https://www.nowcoder.com/practice/1ce93d5cec5c4243930fc5e8efaaca1e
select start_month, count(distinct uid) mau,sum(new) month_add_uv, max(sum(new)) over(order by start_month ), SUM(sum(new))OVER(ORDER BY start_month) cum_sum_uv from( select *,date_format(start_time,'%Y%m') start_month, if(start_time=(min(start_time) over(partition by uid)),1,0) new from exam_record ) t1 group by start_month
思路:
1.每月用户活跃数:按月分组,distinct(uid)
2.每月新增用户数:新增一列标记每个用户是否为第一次登录,是为1,不是为0。
if(start_time=(min(start_time) over(partition by uid)),1,0)
按月分组进行加和
3.截至当月单月最大新增用户数:截至当月新增用户数最大值 max(sum(new)) over(order by start_month )
4.截至当月累计用户数:每个月的新增人数相加