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
全部评论
相关推荐
07-09 16:27
门头沟学院 Java 点赞 评论 收藏
分享
06-12 17:08
天津理工大学 Java 点赞 评论 收藏
分享
牛客刘北:两个缺爱的人是没有办法好好在一起的,但世界上哪有什么是非对错?你后悔你们在一起了,但是刚刚在一起的美好也是真的呀,因为其他人的出现,你开始想要了最开始的自己,你的确对不起自己,21岁的你望高物远,你完全可以不谈恋爱,去过你想要的生活,你向往自由,在一起之后,你要想的不是一个人,而是两个人,你不是变心了,就像你说的,你受够了,你不想包容了,冷静几天是你最优的选择,爱人先爱己。
点赞 评论 收藏
分享