当前位置:网站首页>SQL必会题之留存率
SQL必会题之留存率
2022-07-01 05:45:00 【Begin to change】
目录
一、题目
查询每天新增用户数以及他们次日留存率和三十日留存率
二、步骤
1、先求出每个用户最早的登录日期(辅助列)
SELECT user_id,login_date,MIN(login_date) over (PARTITION by user_id ORDER BY login_date ) as first_login from tb_user_login;
2、连表
将有辅助列的表与原表进行关联,关联的条件不仅为id相等,还有一个条件就是其登录的时间与最早的登录时间的差值
SELECT user_id,login_date,MIN(login_date) over (PARTITION by user_id ORDER BY login_date ) as first_login from tb_user_login)t1
LEFT JOIN tb_user_login as t2
on t1.user_id = t2.user_id and DATEDIFF(t2.login_date,first_login)=1
left JOIN tb_user_login as t3
on t1.user_id = t3.user_id and DATEDIFF(t3.login_date,first_login)=29
GROUP BY first_login,uesr_id;
3、统计
COUNT(DISTINCT t1.user_id) as 新增用户数,
COUNT(DISTINCT t2.user_id) /COUNT(DISTINCT t1.user_id) as 次日留存,
COUNT(DISTINCT t3.user_id) /COUNT(DISTINCT t1.user_id) as 三十日留存
4、源码
SELECT user_id,login_date,MIN(login_date) over (PARTITION by user_id ORDER BY login_date ) as first_login from tb_user_login;
# 查看每个用户登录的日期
SELECT
first_login,
COUNT(DISTINCT t1.user_id) as 新增用户数,
COUNT(DISTINCT t2.user_id) /COUNT(DISTINCT t1.user_id) as 次日留存,
COUNT(DISTINCT t3.user_id) /COUNT(DISTINCT t1.user_id) as 三十日留存
from (SELECT user_id,login_date,MIN(login_date) over (PARTITION by user_id ORDER BY login_date ) as first_login from tb_user_login)t1
LEFT JOIN tb_user_login as t2
on t1.user_id = t2.user_id and DATEDIFF(t2.login_date,first_login)=1
left JOIN tb_user_login as t3
on t1.user_id = t3.user_id and DATEDIFF(t3.login_date,first_login)=29
GROUP BY first_login;
边栏推荐
- [SRS] use of Vhost isolated stream: push / pull Stream Address
- 4GB大文件,如何实时远程传输和共享?
- Mongodb学习篇:安装后的入门第一课
- libpng12.so.0: cannot open shared object file: No such file or directory 亲测有效
- Chip, an empire built on sand!
- Geoffrey Hinton:我的五十年深度学习生涯与研究心法
- Build 2022 上开发者最应关注的七大方向主要技术更新
- 分片上传与断点续传
- Know the future of "edge computing" from the Nobel prize!
- 论文学习记录随笔 多标签之LSML
猜你喜欢
健康照明中应用的LED照明灯
亲爱的派盘用户,我要向您表白!
Know the future of "edge computing" from the Nobel prize!
C语言初阶——实现扫雷游戏
Some errors encountered in MySQL data migration
mysql 将毫秒数转为时间字符串
Don't put your notes and videos everywhere!
Through cooperation with the University of international trade, we can increase efficiency for college students
【知识点总结】卡方分布,t分布,F分布
boot+jsp的高校社团管理系统(附源码下载链接)
随机推荐
He struggled day and night to protect his data
CJC8988带2个立体声耳机驱动器的低功率立体声编解码器
This is the necessary software for college students 𞓜 knowledge management
ssm+mysql二手交易网站(论文+源码获取链接)
Crossing sect · paipan + Siyuan notes = private notebook
Educational administration management system of SSM (free source code)
SSGSSRCSR区别
HDU - 1024 Max Sum Plus Plus(DP)
MySQL converts milliseconds to time string
Dear pie users, I want to confess to you!
基于TI DRV8424驱动步进电机实现调速和行程控制
Ssgssrcsr differences
Don't put your notes and videos everywhere!
多表操作-外键级联操作
表格中el-tooltip 实现换行展示
为什么用葫芦儿派盘取代U盘?
Chip, an empire built on sand!
如何添加葫芦儿派盘
[QT] QT after addition, subtraction, multiplication and division, two decimal places are reserved
OpenGL ES: (3) EGL、EGL绘图的基本步骤、EGLSurface、ANativeWindow