牛客网在线编程
思路:
首先找出用户的注册日期,即date(min(in_time)) 转成date形式
建立两个辅助表,我先放代码,然后进行解释
sql">withuser_reg as (selectuid,date(min(in_time)) as first_datefromtb_user_loggroup by1),user_log as (selectuid,date(in_time) as dtfromtb_user_logunionselectuid,date(out_time) as dtfromtb_user_log)
selectreg.first_date,round(count(log.dt) / count(reg.first_date), 2)
fromuser_reg regleft join user_log log on reg.uid = log.uidand reg.first_date = date_sub(log.dt, interval 1 day)
where reg.first_date like '2021-11%'
group by1
第一张表user_reg表,拿出用户第一天注册时间,通过uid聚合。
第二张表user_log表,链接用户每次登陆过的时间
那么只要用户注册表中的行能链接上用户登陆表的天数减一就代表这个用户第二天还继续登陆,即我们要求的用户留存。