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

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

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

select new_date as dt,round(count(distinct u.uid)/count(distinct temp.uid),2)as uv_left_rate
from(
    select uid,min(date(in_time)) as new_date
    from tb_user_log
    group by uid
) as temp left join tb_user_log as u
     on temp.uid=u.uid 
        and (
            date_add(new_date,interval 1 day)=date(in_time)
            or (new_date=date(in_time) and datediff(out_time,in_time)>0)
         ) 
where new_date like '2021-11-%'
group by new_date
order by dt

1.首先查找所有的新用户,得到新用户的uid、new_date(第一次登录日期)。

select uid,min(date(in_time)) as new_date
    from tb_user_log
    group by uid

2.与tb_user_log进行左连接,连接条件有:

  1. uid相同,temp.uid=u.uid
  2. 第一次登录的第二天进行了登录,date_add(new_date,interval 1 day)=date(in_time)
  3. in_time-进入时间和out_time-离开时间跨天,new_date=date(in_time) and datediff(out_time,in_time)>0

3.筛选2021年11月的新用户,然后进行分组统计数据;考虑到一天可能登录多次,需要使用 distinct 去重

全部评论

相关推荐

野猪不是猪🐗:我assume that你must技术aspect是solid的,temperament也挺good的,however面试不太serious,generally会feel style上不够sharp
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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