当前位置:网站首页>sql计算每日新增用户、及留存率指标
sql计算每日新增用户、及留存率指标
2022-06-28 18:39:00 【南湖渔歌】
show databases;
-- 选择数据库进行建库
use tempt2022;
-- 用户注册表
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');
-- 登陆日志表
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;
# 每日新增DNU,次日留存率,3日留存率,7日留存率
select *
from user_info a
left join login_log b on a.user_id = b.user_id;
/* * 次日留存:1月1日注册的新用户,在1月2日登陆了app,即登陆日期 - 注册日期 = 1天 * 3日留存:1月1日注册的新用户,在1月4日登陆了app,即登陆日期 - 注册日期 = 3天 * 7日留存:1月1日注册的新用户,在1月8日登陆了app,即登陆日期 - 注册日期 = 7天 * */
select date(reg_time) as dt
,count(distinct a.user_id) as 新增用户数
,sum(datediff(login_time,reg_time) = 1) as 次日留存用户数
,sum(datediff(login_time,reg_time) = 3) as 三日留存用户数
,sum(datediff(login_time,reg_time) = 7) as 七日留存用户数
,concat(round(sum(datediff(login_time,reg_time) = 1)/ count(distinct a.user_id)*100,0),'%') as 次日留存率
,concat(round(sum(datediff(login_time,reg_time) = 3)/ count(distinct a.user_id)*100,1),'%') as 三日留存率
,concat(round(sum(datediff(login_time,reg_time) = 7)/ count(distinct a.user_id)*100,2),'%') as 七日留存率
from user_info a
left join login_log b on a.user_id = b.user_id
group by date(reg_time);

边栏推荐
- 618活动季——百数低代码平台特享折扣来临
- OOM out of memory 内存溢出
- 牛津大學教授Michael Wooldridge:AI社區近40年如何看待神經網絡
- sqrt()函数的详解和用法「建议收藏」
- Sound network releases lingfalcon Internet of things cloud platform, which can build sample scenarios in one hour
- Openfire 3.8.2集群配置
- Enhancing steam and engineering education from theory to practice
- Shell脚本批量修改文件目录权限
- 深入解析kubernetes中的选举机制
- 好用、强大的PDF 阅读软件综合评测:PDF Expert 、MarginNote、LiquidText、Notability、GoodNotes、Zotero
猜你喜欢

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

idea其他分支合并到dev分支

Professor Michael Wooldridge of Oxford University: how the AI community views neural networks in the past 40 years

Servlet的使用手把手教学(一)

About Significance Tests

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

OpenHarmony—内核对象事件之源码详解

What are the design requirements for PCB layout and wiring?

Voice network VQA: make the user's subjective experience of unknown video quality in real-time interaction known

Tensorboard Usage Summary
随机推荐
OpenHarmony—内核对象事件之源码详解
数字化转型的1个目标,3大领域,6大因素和9个环节
数据资产为王,如何解析企业数字化转型与数据资产管理的关系?
1 goal, 3 fields, 6 factors and 9 links of digital transformation
1 invalid import format(s) Postman Collection Format v1 is no longer supported and can not be import
Baidu time factor addition
声网发布灵隼物联网云平台 可一小时构建示例场景
深入解析kubernetes中的选举机制
openGauss内核:SQL解析过程分析
几行代码就能实现复杂的 Excel 导入导出,这个工具类真心强大!
匿名函数变量问题
⼤⼚⾯试真题集合
声网 VQA:将实时互动中未知的视频画质用户主观体验变可知
PHP使用栈解决迷宫问题
An in-depth analysis of the election mechanism in kubernetes
双功能交联剂丨Lumiprobe 磺基花青7二羧酸研究
上传文件列表(文件名重复加括号标识)
Opengauss kernel: analysis of SQL parsing process
打破学科之间壁垒的STEAM教育
从理论到实践增强STEAM和工程教育