题解 | #2021年11月每天新用户的次日留存率#

2021年11月每天新用户的次日留存率

https://www.nowcoder.com/practice/1fc0e75f07434ef5ba4f1fb2aa83a450

# 二刷:通用方法 22:25 ~ 22:48   23min
with tb0 as (
    select distinct uid,date(in_time) as in_date from tb_user_log
    union 
    select distinct uid,date(out_time) as in_date from tb_user_log
),
# 1.自连接
tb1 as(
    select a.uid,a.in_date as time1,b.in_date as time2,
    #  标记新用户
    if(min(a.in_date)over(partition by a.uid order by a.in_date) = a.in_date,1,null) as if_new
    from tb0 a left join tb0 b on a.uid = b.uid
),
# 2.计算时间差
tb2 as(
    select uid,timestampdiff(day,time1,time2) as diff_time,time1,time2,if_new
    from tb1
    # 筛选新用户  且 2021年11月
    where if_new = 1
)
# 3.计数符合天数差的作为n日留存数
select time1,
# count(distinct if(diff_time = 1 ,uid,null)),
# count( distinct uid),
round(count(distinct if(diff_time = 1 ,uid,null))/count(distinct uid),2)
from tb2
# 时间筛选
where date_format(time1,'%Y%m') = '202111'
group by time1
having count(distinct uid) != 0 
order by time1







全部评论

相关推荐

不愿透露姓名的神秘牛友
06-27 18:48
点赞 评论 收藏
分享
05-09 12:23
已编辑
华南理工大学 Java
野猪不是猪🐗:给他装的,双九+有实习的能看的上这种厂我直接吃⑨✌们拿它练练面试愣是给他整出幻觉了
点赞 评论 收藏
分享
Java抽象带篮子:简历怎么写可以看看我发的帖子,你的第一个是实习经历吗?那怎么写的是你的第一个练手项目呢?简历写的怎么样直接投小厂面试一下就知道了
没有实习经历,还有机会进...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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