题解 | #2021年11月每天新用户的次日留存率#
2021年11月每天新用户的次日留存率
https://www.nowcoder.com/practice/1fc0e75f07434ef5ba4f1fb2aa83a450
--统计2021年11月每天新用户的次日留存率(保留2位小数)
--注:
--次日留存率为当天新增的用户数中第二天又活跃了的用户数占比。
--如果in_time-进入时间和out_time-离开时间跨天了,在两天里都记为该用户活跃过,结果按日期升序。
-- 分析:
-- 1、求出每日的新增用户
-- 2、求出用户全量表(如果in_time-进入时间和out_time-离开时间跨天了,在两天里都记为该用户活跃过)
select first_login_time dt,
round(count(distinct t2.uid)/count(t1.uid),2)
from
(
求出每日的新增用户
select uid,min(date(in_time)) first_login_time
from tb_user_log
group by uid
)t1
left join
(
求出用户全量表
select uid,date(in_time) td
from tb_user_log
union
select uid,date(out_time)
from tb_user_log
)t2
on t1.uid=t2.uid
and datediff(td,first_login_time)=1
group by first_login_time
having date_format(first_login_time,'%Y-%m')='2021-11'
order by dt;
