题解 | #2021年11月每天新用户的次日留存率#
2021年11月每天新用户的次日留存率
https://www.nowcoder.com/practice/1fc0e75f07434ef5ba4f1fb2aa83a450
运用with建立了一个用户登陆表,再建立一个用户注册表。利用右外连接来对用户的登录时间注册时间的关系进行了一个比对;
具体如下代码与注释解析:在建立了两个表之后,可知先用uid来进行一层连接,其次最重要的是保存注册之后一天仍然登陆的用户,即“注册日期+1=登陆日期”的用户:
date_sub(cus_1.Time,interval 1 day) = cus_2.Time详细代码:
with cus_1(uid,Time) as (select uid,date_format(in_time,'%Y-%m-%d')Time from tb_user_log union select uid,date_format(out_time,'%Y-%m-%d')Time from tb_user_log order by Time,uid) #用户登陆表 select cus_2.Time as dt,round(count(cus_1.uid)/count(cus_2.uid),2) from cus_1 right join (select uid,min(date(in_time)) Time from tb_user_log group by uid)cus_2 #用户注册表 on cus_1.uid =cus_2.uid and date_sub(cus_1.Time,interval 1 day) = cus_2.Time where date_format(cus_2.Time,'%Y-%m') = '2021-11' group by cus_2.Time order by dt