当前位置:网站首页>SQL calculates daily new users and retention rate indicators
SQL calculates daily new users and retention rate indicators
2022-06-28 19:02:00 【South Lake Fishing Song】
show databases;
-- Select a database to build a database
use tempt2022;
-- User registry
create table user_info(user_id varchar(10) primary key,reg_time datetime);
insert into user_info values
('u_01','2020-01-01 09:15:00'),
('u_02','2020-01-01 00:04:00'),
('u_03','2020-01-01 22:16:00'),
('u_04','2020-01-01 20:32:00'),
('u_05','2020-01-01 13:59:00'),
('u_06','2020-01-01 21:28:00'),
('u_07','2020-01-01 14:03:00'),
('u_08','2020-01-01 11:00:00'),
('u_09','2020-01-01 23:57:00'),
('u_10','2020-01-01 04:46:00'),
('u_11','2020-01-02 14:21:00'),
('u_12','2020-01-02 11:15:00'),
('u_13','2020-01-02 07:26:00'),
('u_14','2020-01-02 10:34:00'),
('u_15','2020-01-02 08:22:00'),
('u_16','2020-01-02 14:23:00'),
('u_17','2020-01-03 09:20:00'),
('u_18','2020-01-03 11:21:00'),
('u_19','2020-01-03 12:17:00'),
('u_20','2020-01-03 15:26:00');
-- Login log table
create table login_log(user_id varchar(10),login_time datetime,primary key(user_id,login_time));
insert into login_log values
('u_02','2020-01-02 00:14:00'),
('u_10','2020-01-02 08:32:00'),
('u_03','2020-01-02 09:20:00'),
('u_08','2020-01-02 10:07:00'),
('u_04','2020-01-02 10:29:00'),
('u_09','2020-01-02 11:45:00'),
('u_05','2020-01-02 12:19:00'),
('u_01','2020-01-02 14:29:00'),
('u_15','2020-01-03 00:26:00'),
('u_14','2020-01-03 11:18:00'),
('u_11','2020-01-03 13:18:00'),
('u_16','2020-01-03 14:33:00'),
('u_06','2020-01-04 07:51:00'),
('u_18','2020-01-04 08:11:00'),
('u_07','2020-01-04 09:27:00'),
('u_10','2020-01-04 10:59:00'),
('u_20','2020-01-04 11:51:00'),
('u_03','2020-01-04 12:37:00'),
('u_17','2020-01-04 15:07:00'),
('u_08','2020-01-04 16:35:00'),
('u_01','2020-01-04 19:29:00'),
('u_14','2020-01-05 08:03:00'),
('u_12','2020-01-05 10:27:00'),
('u_15','2020-01-05 16:33:00'),
('u_19','2020-01-06 09:03:00'),
('u_20','2020-01-06 15:26:00'),
('u_04','2020-01-08 11:03:00'),
('u_05','2020-01-08 12:54:00'),
('u_06','2020-01-08 19:22:00'),
('u_13','2020-01-09 10:20:00'),
('u_15','2020-01-09 16:40:00'),
('u_18','2020-01-10 21:34:00');
show tables;
select * from user_info limit 10;
# Add... Every day DNU, Next day retention rate ,3 Daily retention rate ,7 Daily retention rate
select *
from user_info a
left join login_log b on a.user_id = b.user_id;
/* * The next day :1 month 1 New users registered on the th , stay 1 month 2 It's the day of landing app, I.e. login date - Registration date = 1 God * 3 Daily retention :1 month 1 New users registered on the th , stay 1 month 4 It's the day of landing app, I.e. login date - Registration date = 3 God * 7 Daily retention :1 month 1 New users registered on the th , stay 1 month 8 It's the day of landing app, I.e. login date - Registration date = 7 God * */
select date(reg_time) as dt
,count(distinct a.user_id) as Number of new users
,sum(datediff(login_time,reg_time) = 1) as Number of users retained in the next day
,sum(datediff(login_time,reg_time) = 3) as Number of users retained in three days
,sum(datediff(login_time,reg_time) = 7) as Number of users retained in seven days
,concat(round(sum(datediff(login_time,reg_time) = 1)/ count(distinct a.user_id)*100,0),'%') as Next day retention rate
,concat(round(sum(datediff(login_time,reg_time) = 3)/ count(distinct a.user_id)*100,1),'%') as Three day retention rate
,concat(round(sum(datediff(login_time,reg_time) = 7)/ count(distinct a.user_id)*100,2),'%') as Seven day retention rate
from user_info a
left join login_log b on a.user_id = b.user_id
group by date(reg_time);

边栏推荐
- Graduation project - Design and development of restaurant management game based on unity (with source code, opening report, thesis, defense PPT, demonstration video and database)
- Taishan Office Technology Lecture: word strange font height
- Mybayis之核心主件分析
- C# 41. int与string互转
- Analysis of the core components of mybayis
- leetcode 1689. Partitioning into minimum number of deci binary numbers
- Question brushing analysis tool
- 深入解析kubernetes中的选举机制
- Yixin Huachen: real estate enterprises want to grasp the opportunity of the times for digital transformation
- WiFi安全漏洞KRACK深度解读
猜你喜欢

1 goal, 3 fields, 6 factors and 9 links of digital transformation

C#连接数据库完成增删改查操作

openGauss内核:SQL解析过程分析

180.1. Log in continuously for n days (database)

shell读取Json文件的值

月环比sql实现

POI Excel转换工具

MongoDB系列之MongoDB工作原理简单介绍

leetcode 1423. Maximum points you can obtain from cards

Sound network releases lingfalcon Internet of things cloud platform, which can build sample scenarios in one hour
随机推荐
C#连接数据库完成增删改查操作
【C#】详解值类型和引用类型区别
中金财富开户安全吗?开过中金财富的讲一下
618 activity season - the arrival of special discounts for hundreds of low code platforms
获取当前日期0点及23点59时间戳
render函数解析
About Statistical Distributions
1 goal, 3 fields, 6 factors and 9 links of digital transformation
Shell script batch modify file directory permissions
OpenHarmony—内核对象事件之源码详解
Collection of real test questions
sql计算每日新增用户、及留存率指标
Windows 64位下载安装My SQL
leetcode 1689. Partitioning into minimum number of deci binary numbers
19.2 container classification, array and vector container refinement
180.1.连续登录N天(数据库)
Anonymous function this pointing and variable promotion
sqrt()函数的详解和用法「建议收藏」
Memory leak
内存泄露