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
全部评论

相关推荐

点赞 评论 收藏
分享
01-26 18:45
门头沟学院 Java
一天代码十万三:哥们实习再包一下吧,产出太笼统了,尽量体现业务
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务