当前位置:网站首页>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);

边栏推荐
猜你喜欢

About Significance Tests

Hands on Teaching of servlet use (1)

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

Opengauss kernel: analysis of SQL parsing process

CVPR2022 | 浙大、蚂蚁集团提出基于标签关系树的层级残差多粒度分类网络,建模多粒度标签间的层级知识

数字化转型的1个目标,3大领域,6大因素和9个环节

匿名函数this指向以及变量提升

About covariance and correlation

应用实践 | 10 亿数据秒级关联,货拉拉基于 Apache Doris 的 OLAP 体系演进(附 PPT 下载)

微信小程序接入百度统计报错 Cannot read property ‘mtj‘ of undefined
随机推荐
Voice network VQA: make the user's subjective experience of unknown video quality in real-time interaction known
Analysis of the core components of mybayis
[unity3d] camera follow
Windows 64位下载安装My SQL
内存泄露
Professor Michael Wooldridge of Oxford University: how the AI community views neural networks in the past 40 years
1 goal, 3 fields, 6 factors and 9 links of digital transformation
sqrt()函数的详解和用法「建议收藏」
1 invalid import format(s) Postman Collection Format v1 is no longer supported and can not be import
使用.NetCore自带的后台作业,出入队简单模拟生产者消费者处理请求响应的数据
几行代码就能实现复杂的 Excel 导入导出,这个工具类真心强大!
devpi
Pipeline based hybrid rendering
A few lines of code can realize complex excel import and export. This tool class is really powerful!
180.1. Log in continuously for n days (database)
Win 10创建一个gin框架的项目
百度时间因子添加
POI excel conversion tool
微博评论的高性能高可用计算架构方案
ANR Application Not Responding