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

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

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

with td as (
    select id,uid,date(in_time) as indate,date(out_time) as outdate
    from tb_user_log 
),

tnew as (
    select t.indate,count(t.uid) as nb_new
    from td t
    where not exists (
        select 1 
        from td tback
        where tback.uid=t.uid and tback.indate < t.indate
    )
    group by t.indate
)

select t.indate, round(sum(case when exists(
        select 1
        from td tafter
        where t.uid=tafter.uid and datediff(tafter.indate,t.indate)=1
     ) or (datediff(t.outdate,t.indate)=1) 
     then 1 else 0 end) /avg(tn.nb_new),2)
from td t left join tnew tn on t.indate=tn.indate
where not exists (
        select 1 
        from td tback
        where tback.uid=t.uid and tback.indate < t.indate
    ) 
    and t.indate between'2021-11-1' and '2021-11-30'
group by t.indate
order by t.indate asc






全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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