SQL144 每月及截止当月的答题情况
SQL144 每月及截止当月的答题情况
1、使用排名函数,根据uid进行分组,start_time进行升序;这样就可以实现排名为1即为该用户第一次登录
2、根据月份分组求出排名为1个数即可得出该月新增用户数。
3、然后对新增用户数进行max()开窗over(),并且根据月份升序,就可以得出截止当月的单月历史最大新增用户数
4、同理,然后对新增用户数进行sum()开窗over(),并且根据月份升序,就可以得出截止当月的累积用户数
# 在这里的开窗函数中,因为涉及到历史的数据,故不能使用partition by进行分区
with t1 as(
select
uid,date_format(start_time,'%Y%m') start_month
,row_number() over(partition by uid order by start_time) ranking
from
exam_record
)
select
start_month,count(distinct uid) mau
,sum(if(ranking=1,1,0)) month_add_uv
,max(sum(if(ranking=1,1,0))) over(order by start_month) max_month_uv
,sum(sum(if(ranking=1,1,0))) over(order by start_month) cum_sum_uv
from
t1
group by
start_month
order by start_month
1、使用排名函数,根据uid进行分组,start_time进行升序;这样就可以实现排名为1即为该用户第一次登录
2、根据月份分组求出排名为1个数即可得出该月新增用户数。
3、然后对新增用户数进行max()开窗over(),并且根据月份升序,就可以得出截止当月的单月历史最大新增用户数
4、同理,然后对新增用户数进行sum()开窗over(),并且根据月份升序,就可以得出截止当月的累积用户数
# 在这里的开窗函数中,因为涉及到历史的数据,故不能使用partition by进行分区
with t1 as(
select
uid,date_format(start_time,'%Y%m') start_month
,row_number() over(partition by uid order by start_time) ranking
from
exam_record
)
select
start_month,count(distinct uid) mau
,sum(if(ranking=1,1,0)) month_add_uv
,max(sum(if(ranking=1,1,0))) over(order by start_month) max_month_uv
,sum(sum(if(ranking=1,1,0))) over(order by start_month) cum_sum_uv
from
t1
group by
start_month
order by start_month
全部评论
相关推荐
点赞 评论 收藏
分享
01-26 18:45
门头沟学院 Java 点赞 评论 收藏
分享
![](https://static.nowcoder.com/fe/file/oss/1716965564844UEBJN.png)
![](https://static.nowcoder.com/fe/file/oss/1716965585666UBBME.png)
晟通集团
| 校招
| 23个岗位
点赞 评论 收藏
分享