sql 怎么统计一段时间的用户数_SQL 计算留存率-链家面试题

本文介绍了如何使用SQL来统计一段时间内的活跃用户数和计算留存率,重点在于自联结表格和CASE WHEN语句的运用。通过案例分析了手机厂商如何分析相机应用的用户行为,提供了具体的SQL解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

8d113bde2bf18c8d255153af2654b3a8.png

感谢猴子的SQL面试系列课程

免费教程《图解SQL面试题》​mp.weixin.qq.com
f3dce86b084f91366e8c89107f2ffcb2.png

将数据导入navicat

手机中的相机是深受大家喜爱的应用之一,下图是某手机厂商数据库中的用户行为信息表中部分数据的截图

d6546a80ce9fcdda3fd74962686f4b0e.png

现在该手机厂商想要分析手机中的应用(相机)的活跃情况,需统计如下数据:

需要获得的数据的格式如下:

eeb98234cab16c04328196e5ca7663bc.png

计算时间间隔类问题,使用自联结

首先计算活跃用户数

select 登陆时间 ,count(distinct 用户id) as 活跃用户数 
from 登录信息
where 应用名称 ='相机' 
group by 登陆时间

7915e835f7cc7b9d9cef3b880a8cc4b5.png

将表进行自连接,得到如下形式的格式对于计算留存率十分关键

select a.`用户`,a.登陆时间 as a_t ,b.登陆时间 as b_t
from 登录信息 as a  
left join 登录信息 as b
on a.`用户`=b.`用户`
where a.应用名称= '相机' AND b.应用名称='相机';

2e564af1af02bc2462a855135c999e9f.png

将上表存成视图C

再计算时间间隔

select *,timestampdiff(day,a_t,b_t) 
         as 时间间隔
from C

98aabedd8052619638a84c89272d85ee.png

使用case when 计算次日留存

select d.a_t,count(distinct case when d.时间间隔=1 then d.用户id     
               else null
               end) as  次日留存数 
from
(select *,timestampdiff(day,a_t,b_t) as 时间间隔
from c) as d
group by d.a_t;

e9df8c539f130f785931e84909cfbb24.png
select d.a_t,count(distinct case when d.时间间隔=1 then d.用户id     
               else null
               end) as  次日留存数, 
count(distinct case when 时间间隔=1 then d.用户id
               else null
               end) /count(distinct d.用户id) as 次日留存率,
count(distinct case when d.时间间隔=3 then d.用户id     
               else null
               end) as  3日留存数 ,
count(distinct case when 时间间隔=3 then d.用户id
               else null
               end) /count(distinct d.用户id) as 3日留存率,
count(distinct case when d.时间间隔=7 then d.用户id     
               else null
               end) as  7日留存数 ,
count(distinct case when 时间间隔=7 then d.用户id
               else null
               end) /count(distinct d.用户id) as 7日留存率

from
(select *,timestampdiff(day,a_t,b_t) as 时间间隔
from c) as d
group by d.a_t; 

601de4d8bc750aa4255780944e647f55.png

再整合起来

select d.a_t,count(distinct case when d.时间间隔=1 then d.用户id     
               else null
               end) as  次日留存数, 
count(distinct case when 时间间隔=1 then d.用户id
               else null
               end) /count(distinct d.用户id) as 次日留存率,
count(distinct case when d.时间间隔=3 then d.用户id     
               else null
               end) as  3日留存数 ,
count(distinct case when 时间间隔=3 then d.用户id
               else null
               end) /count(distinct d.用户id) as 3日留存率,
count(distinct case when d.时间间隔=7 then d.用户id     
               else null
               end) as  7日留存数 ,
count(distinct case when 时间间隔=7 then d.用户id
               else null
               end) /count(distinct d.用户id) as 7日留存率

from
(select *,timestampdiff(day,a_t,b_t) as 时间间隔
from (select a.`用户id`,a.登陆时间 as a_t ,b.登陆时间 as b_t
from 登录信息 as a  
left join 登录信息 as b
on a.`用户id`=b.`用户id`
where a.应用名称= '相机' AND b.应用名称='相机') as c) as d
group by d.a_t; 

留存率类问题关键点:

得到自联结的表十分重要,是后面计算时间间隔的关键

分组统计一定要使用 case when 语句

再次感谢猴子老师的练习数据和参考思路。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值