当前位置:网站首页>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
边栏推荐
- 损失函数与正负样本分配:YOLO系列
- 14_ Redis_ Optimistic lock
- Key points of compilation principle examination in 2021-2022 academic year [overseas Chinese University]
- MySQL -- Index Optimization -- order by
- [C language] explain the initial and advanced levels of the pointer and points for attention (1)
- CDN 在游戏领域的应用
- LeetCode_ Sliding window_ Medium_ 395. Longest substring with at least k repeated characters
- HUSTPC2022
- FPGA - clock-03-clock management module (CMT) of internal structure of 7 Series FPGA
- Yolo format data set processing (XML to txt)
猜你喜欢
![[noi simulation] Elis (greedy, simulation)](/img/a2/f8c8ab3bc8dd779327be3f76990976.png)
[noi simulation] Elis (greedy, simulation)

Base64 coding can be understood this way

15_ Redis_ Redis. Conf detailed explanation

Practical debugging skills

18_ Redis_ Redis master-slave replication & cluster building

Equipped with Ti am62x processor, Feiling fet6254-c core board is launched!

15_Redis_Redis.conf详解

N皇后问题的解决

FPGA - clock-03-clock management module (CMT) of internal structure of 7 Series FPGA

Application and practice of Jenkins pipeline
随机推荐
JVM architecture, classloader, parental delegation mechanism
Be a good gatekeeper on the road of anti epidemic -- infrared thermal imaging temperature detection system based on rk3568
02_ Linear table_ Sequence table
MySQL -- Index Optimization -- order by
XML配置文件
10_Redis_geospatial_命令
20_Redis_哨兵模式
Real estate market trend outlook in 2022
Points clés de l'examen de principe de compilation pour l'année scolaire 2021 - 2022 [Université chinoise d'outre - mer]
Base64 coding can be understood this way
记一次面试
Jenkins Pipeline 应用与实践
Application and practice of Jenkins pipeline
Set set you don't know
Build your own semantic segmentation platform deeplabv3+
03.golang初步使用
15_Redis_Redis.conf详解
Infra11199 database system
How to choose a third-party software testing organization for automated acceptance testing of mobile applications
15_ Redis_ Redis. Conf detailed explanation