题解 | 临时表——可读性好

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

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

with new_user_t as (
    SELECT *
    from tb_user_log
    where (uid,in_time) in (
      select uid,min(in_time)
      from tb_user_log
      group by uid
        )
    and date_format(in_time,'%Y-%m')='2021-11'
), -- 每天新用户表
active_user_t as (
    select uid,in_time
    from tb_user_log
    where date_format(in_time,'%Y-%m')='2021-11'
    union
    select uid,out_time
    from tb_user_log
    where date_format(in_time,'%Y-%m')='2021-11'
) -- 每天活跃用户表
select date(b.in_time) dt
,round(count(distinct case when datediff(a.in_time,b.in_time)=1 then b.uid end)/count(distinct b.uid),2) 次日留存率
from active_user_t a
inner join new_user_t b
on a.uid=b.uid
group by date(b.in_time)
order by dt
全部评论

相关推荐

12-05 18:09
已编辑
广东药科大学 后端工程师
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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