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

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

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

改进:针对即使是跨多天的情况也没问题!

使用到recursive 递归函数

 with recursive b as (  #活跃用户表。使用递归函数,显示出用户的所有in_time-进入时间和out_time-离开时间的日期(即使跨多天,也没问题!)
select uid,date(in_time) as dt,date(out_time) as ddt from tb_user_log
union all 
select uid,dt,date_sub(ddt,interval 1 day)
from b where ddt>dt
)

select a.min_dt as dt,
round(count(distinct b.uid)/count(distinct a.uid),2) as uv_left_rate
from (
    select uid,min(date(in_time)) as min_dt   #2021年11月的新用户表
    from tb_user_log
    group by uid
    having min(date(in_time)) between '2021-11-01' and '2021-11-30'
) as a
left join b                       #左连接          
on datediff(b.ddt,a.min_dt)=1 and a.uid=b.uid  #两个连接条件!
group by a.min_dt 
order by dt

全部评论

相关推荐

粗心的熊熊求求offer:什么内容都没有还弄两页
点赞 评论 收藏
分享
06-02 15:17
门头沟学院 Java
心爱的idea:怎么会呢 应该是打招呼有问题 问就说实习6个月全国可飞随时到岗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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