题解 | #2021年11月每天新用户的次日留存率#

2021年11月每天新用户的次日留存率

https://www.nowcoder.com/practice/1fc0e75f07434ef5ba4f1fb2aa83a450

#小白新思路,好理解,利用窗口函数直接求出结果。
#1.先合并表,因题目说了离开日期为第二天也算隔天登录。
#2.先找到第一次登录时间,因按照天计算,可以形成1天多次登录,第一天多次登录都算第一次登录时间。
#3.利用lead()求出下次登录时间,因要求次日留存率,故需要。
#4.筛出2021年11月份,排名第一次登录的时间,与lead()的结果差=1,就是隔天的人数count(distinct uid).
#5.相除就能得到答案。
with tiaojian as (
select 
uid,
date(in_time) as pday
from tb_user_log
union all
select
uid,
date(out_time) as pday
from tb_user_log
)

select 
t.pday,
round(
count(distinct case when datediff(t.y,t.pday)=1 then uid end)/
count(distinct uid),2) as uv_left_rate

from(
select 
uid,
pday,
lead(pday,1)over(partition by uid order by pday) as y,
dense_rank()over(partition by uid order by pday) as m
from tiaojian
) as t 
where
t.m=1
and
date_format(t.pday,"%Y%m")=202111
group by t.pday
order by t.pday

全部评论

相关推荐

头顶尖尖的程序员:我也是面了三四次才放平心态的。准备好自我介绍,不一定要背熟,可以记事本写下来读。全程控制语速,所有问题都先思考几秒,不要急着答,不要打断面试官说话。
点赞 评论 收藏
分享
身边有人上海、深圳 6、7k 都去了,真就带薪上班了。
程序员小白条:木的办法, 以后越来越差,还是家附近宅着吧,毕业的人越来越多,岗位都提供不出来,经济又过了人口红利期
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务