当前位置:网站首页>MySQL calculate n-day retention rate
MySQL calculate n-day retention rate
2022-07-02 15:26:00 【Caspian�】
One 、 Create a table containing the earliest login date of each user
select user_id,min(date) as first_day
from a2_userbehavior_csv
group by user_idTwo 、 Create a table containing all login dates of each user
In fact, it means de duplication of users and dates
select user_id,date
from a2_userbehavior_csv
group by user_id,date3、 ... and 、 Set the two tables according to user_id Splicing , And calculate the date time difference
select t1.*,t2.date,datediff(t2.date,t1.first_day) as day_diff
from
(select user_id,min(date) as first_day
from a2_userbehavior_csv
group by user_id) as t1
left join
(select user_id,date
from a2_userbehavior_csv
group by user_id,date) as t2
on t1.user_id=t2.user_idThe results are as follows :

Get the number of days from each login date of each user to its earliest login date .
Four 、 Calculate various retention rates
Now the train of thought is clear .
Next day retention rate =(day_diff=1 The number of )/(day_diff=0 The number of )
Three day retention rate =(day_diff=3 The number of )/(day_diff=0 The number of )
...
select first_day as dt,
concat(round(100*count(case when day_diff=1 then user_id end)/count(case when day_diff=0 then user_id end),2),"%") as ' Next day retention rate ',
concat(round(100*count(case when day_diff=3 then user_id end)/count(case when day_diff=0 then user_id end),2),"%") as ' Three day retention rate ',
concat(round(100*count(case when day_diff=7 then user_id end)/count(case when day_diff=0 then user_id end),2),"%") as ' Seven day retention rate '
from
(select t1.*,t2.date,datediff(t2.date,t1.first_day) as day_diff
from
(select user_id,min(date) as first_day
from a2_userbehavior_csv
group by user_id) as t1
left join
(select user_id,date
from a2_userbehavior_csv
group by user_id,date) as t2
on t1.user_id=t2.user_id) as t3
group by first_day
order by first_day
边栏推荐
- Practice of compiling principle course -- implementing an interpreter or compiler of elementary function operation language
- Markdown tutorial
- . Solution to the problem of Chinese garbled code when net core reads files
- 20_ Redis_ Sentinel mode
- Oracle primary key auto increment
- Common English abbreviations for data analysis (I)
- Deploy tidb cluster with tiup
- Internet Explorer officially retired
- Learn the method code of using PHP to realize the conversion of Gregorian calendar and lunar calendar
- AtCoder Beginner Contest 254
猜你喜欢

工程师评测 | RK3568开发板上手测试

yolo格式数据集处理(xml转txt)

.NET Core 日志系统

The past and present lives of visual page building tools

【网络安全】网络资产收集

基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测

21_ Redis_ Analysis of redis cache penetration and avalanche

Practice of compiling principle course -- implementing an interpreter or compiler of elementary function operation language

15_Redis_Redis.conf详解

vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)
随机推荐
Yolo format data set processing (XML to txt)
Real estate market trend outlook in 2022
原则、语言、编译、解释
17_Redis_Redis发布订阅
TiDB数据迁移场景综述
yolo格式数据集处理(xml转txt)
The past and present lives of visual page building tools
牛客练习赛101
12_Redis_Bitmap_命令
让您的HMI更具优势,FET-G2LD-C核心板是个好选择
基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测
Case introduction and problem analysis of microservice
做好抗“疫”之路的把关人——基于RK3568的红外热成像体温检测系统
13_Redis_事务
数据分析思维分析方法和业务知识——业务指标
Yolov5 code reproduction and server operation
Jenkins Pipeline 应用与实践
08_ 串
Tidb environment and system configuration check
11_ Redis_ Hyperloglog_ command