当前位置:网站首页>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;
边栏推荐
- 不是你脑子不好用,而是因为你没有找到对的工具
- Don't put your notes and videos everywhere!
- 论文学习记录随笔 多标签之LIFT
- Leetcode top 100 question 2 Add two numbers
- Educational administration management system (free source code)
- OpenGL ES: (2) OpenGL ES 与 EGL、GLSL的关系
- 激活函数简述
- C语言初阶——实现扫雷游戏
- 从底层结构开始学习FPGA----RAM IP的定制与测试
- OpenGL ES: (3) EGL、EGL绘图的基本步骤、EGLSurface、ANativeWindow
猜你喜欢
Geoffrey Hinton:我的五十年深度学习生涯与研究心法
In depth understanding of condition source code interpretation and analysis of concurrent programming
喊我们大学生个人云服务特供商
健康照明中应用的LED照明灯
Multi table operation - foreign key cascade operation
Crossing sect · paipan + Siyuan notes = private notebook
Ucosiii --- engineering transplantation
MySQL数据迁移遇到的一些错误
【考研高数 自用】高数第一章基础阶段思维导图
Chapitre d'apprentissage mongodb: Introduction à la première leçon après l'installation
随机推荐
OpenGL ES: (1) OpenGL ES的由来 (转)
激活函数简述
穿越派·派盘 + Mountain Duck = 数据本地管理
如何添加葫芦儿派盘
Fiber Bragg grating (FBG) notes [1]: waveguide structure and Bragg wavelength derivation
从底层结构开始学习FPGA----RAM IP的定制与测试
轩逸保养手册
JDBC common interview questions
rust猜数字游戏
Ssm+mysql second-hand trading website (thesis + source code access link)
Qt编写自定义控件-自绘电池
Leetcode top 100 question 2 Add two numbers
Trust guessing numbers game
[SRS] use of Vhost isolated stream: push / pull Stream Address
Typeorm framework
Educational administration management system (free source code)
2022 the 8th China International "Internet +" college student innovation and entrepreneurship competition industry proposition track is open for registration!
Know the future of "edge computing" from the Nobel prize!
Data governance: data governance management (Part V)
【考研高数 自用】高数第一章基础阶段思维导图