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

相关推荐

简历求拷打,海投简历发过去就已读不回了求大佬们指点
程序员牛肉:基本不能了,估计你得放弃秋招,九月份找实习之后明年的春招开始正式找工作
点赞 评论 收藏
分享
每晚夜里独自颤抖:你cet6就cet6,cet4就cet4,你写个cet证书等是什么意思。专业技能快赶上项目行数,你做的这2个项目哪里能提现你有这么多技能呢
点赞 评论 收藏
分享
流浪的神仙:无恶意,算法一般好像都得9硕才能干算法太卷啦
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务