题解 | #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-17 21:57
门头沟学院 Java
白友:噗嗤,我发现有些人事就爱发这些,明明已读不回就行了,就是要恶心人
点赞 评论 收藏
分享
牛客848095834号:举报了
点赞 评论 收藏
分享
xdm怎么说 要被拷打了 担心是KPI
丹田:面就完了,就当日薪四位数的大佬免费给给你面试。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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