题解 | #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进行左连接,连接条件有:
- uid相同,temp.uid=u.uid
- 第一次登录的第二天进行了登录,date_add(new_date,interval 1 day)=date(in_time)
- in_time-进入时间和out_time-离开时间跨天,new_date=date(in_time) and datediff(out_time,in_time)>0
3.筛选2021年11月的新用户,然后进行分组统计数据;考虑到一天可能登录多次,需要使用 distinct 去重。