题解 | #2021年11月每天新用户的次日留存率#
2021年11月每天新用户的次日留存率
https://www.nowcoder.com/practice/1fc0e75f07434ef5ba4f1fb2aa83a450
select t5.dt,round(if(t3.cnt is null,0,t3.cnt)/t5.cnt,2) rate from ( select t2.dt,count(*) cnt from ( select uid,date(in_time) dt from tb_user_log where date_format(in_time,'%Y-%m') = '2021-11' union select uid,date(out_time) dt from tb_user_log where date_format(out_time,'%Y-%m') = '2021-11' )t1 join ( select uid,date(min(in_time)) dt from tb_user_log group by uid )t2 on t1.uid = t2.uid where datediff(t1.dt,t2.dt) = 1 group by t2.dt )t3 right join ( select dt,count(*) cnt from ( select uid,date(min(in_time)) dt from tb_user_log group by uid )t4 group by dt )t5 on t3.dt = t5.dt where date_format(t5.dt,'%Y-%m') = '2021-11' order by t5.dt;