SQL165. 统计活跃间隔对用户分级结果
描述
用户行为日志表tb_user_log
id | uid | artical_id | in_time | out_time | sign_cin |
1 | 109 | 9001 | 2021-08-31 10:00:00 | 2021-08-31 10:00:09 | 0 |
2 | 109 | 9002 | 2021-11-04 11:00:55 | 2021-11-04 11:00:59 | 0 |
3 | 108 | 9001 | 2021-09-01 10:00:01 | 2021-09-01 10:01:50 | 0 |
4 | 108 | 9001 | 2021-11-03 10:00:01 | 2021-11-03 10:01:50 | 0 |
5 | 104 | 9001 | 2021-11-02 10:00:28 | 2021-11-02 10:00:50 | 0 |
6 | 104 | 9003 | 2021-09-03 11:00:45 | 2021-09-03 11:00:55 | 0 |
7 | 105 | 9003 | 2021-11-03 11:00:53 | 2021-11-03 11:00:59 | 0 |
8 | 102 | 9001 | 2021-10-30 10:00:00 | 2021-10-30 10:00:09 | 0 |
9 | 103 | 9001 | 2021-10-21 10:00:00 | 2021-10-21 10:00:09 | 0 |
10 | 101 | 0 | 2021-10-01 10:00:00 | 2021-10-01 10:00:42 | 1 |
user_grade | ratio |
忠实用户 | 0.43 |
新晋用户 | 0.29 |
沉睡用户 | 0.14 |
流失用户 | 0.14 |
示例1
输入:
DROP TABLE IF EXISTS tb_user_log; CREATE TABLE tb_user_log ( id INT PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID', uid INT NOT NULL COMMENT '用户ID', artical_id INT NOT NULL COMMENT '视频ID', in_time datetime COMMENT '进入时间', out_time datetime COMMENT '离开时间', sign_in TINYINT DEFAULT 0 COMMENT '是否签到' ) CHARACTER SET utf8 COLLATE utf8_bin; INSERT INTO tb_user_log(uid, artical_id, in_time, out_time, sign_in) VALUES (109, 9001, '2021-08-31 10:00:00', '2021-08-31 10:00:09', 0), (109, 9002, '2021-11-04 11:00:55', '2021-11-04 11:00:59', 0), (108, 9001, '2021-09-01 10:00:01', '2021-09-01 10:01:50', 0), (108, 9001, '2021-11-03 10:00:01', '2021-11-03 10:01:50', 0), (104, 9001, '2021-11-02 10:00:28', '2021-11-02 10:00:50', 0), (104, 9003, '2021-09-03 11:00:45', '2021-09-03 11:00:55', 0), (105, 9003, '2021-11-03 11:00:53', '2021-11-03 11:00:59', 0), (102, 9001, '2021-10-30 10:00:00', '2021-10-30 10:00:09', 0), (103, 9001, '2021-10-21 10:00:00', '2021-10-21 10:00:09', 0), (101, 0, '2021-10-01 10:00:00', '2021-10-01 10:00:42', 1);
输出:
忠实用户|0.43 新晋用户|0.29 沉睡用户|0.14 流失用户|0.14
Mysql 解法, 执行用时: 36ms, 内存消耗: 6396KB, 提交时间: 2021-12-31
select user_grade, round(count(uid)/(select count(distinct uid) from tb_user_log),2) ratio from -- -------------------------------------------------- (select uid, (case when datediff(td,last_active)<=6 and datediff(td,first_reg)>6 then '忠实用户' when datediff(td,last_active)<=6 and datediff(td,first_reg)<=6 then '新晋用户' when datediff(td,last_active)>6 and datediff(td,last_active)<=29 then '沉睡用户' when datediff(td,last_active)>29 then '流失用户' else '未分类' end) user_grade from -- -------------------------------------------------- (select uid, max(out_time) last_active, min(in_time) first_reg, (select max(out_time) from tb_user_log) td from tb_user_log group by uid) a) b -- -------------------------------------------------- -- -------------------------------------------------- group by user_grade order by ratio desc
Mysql 解法, 执行用时: 36ms, 内存消耗: 6480KB, 提交时间: 2022-01-22
select user_grade,round(count(uid)/(select count(distinct uid) from tb_user_log),2) q from ( select uid,(case when datediff((select max(in_time) from tb_user_log),max(in_time)) <=6 and datediff((select max(in_time) from tb_user_log),min(in_time)) >6 then '忠实用户' when datediff((select max(in_time) from tb_user_log),max(in_time)) <=6 and datediff((select max(in_time) from tb_user_log),min(in_time)) <=6 then '新晋用户' when datediff((select max(in_time) from tb_user_log),max(in_time)) >6 and datediff((select max(in_time) from tb_user_log),min(in_time)) <=29 then '沉睡用户' else '流失用户' end ) user_grade from tb_user_log group by uid ) f1 group by user_grade order by q desc
Mysql 解法, 执行用时: 36ms, 内存消耗: 6504KB, 提交时间: 2021-12-27
WITH today AS( SELECT MAX(DATE(in_time)) FROM tb_user_log #今天的日期 ) SELECT user_grade, ROUND(COUNT(uid) / (SELECT COUNT(DISTINCT uid) FROM tb_user_log), 2) ratio FROM ( SELECT uid, CASE WHEN MAX(DATE(in_time)) BETWEEN DATE_SUB((SELECT * FROM today), INTERVAL 6 DAY) AND (SELECT * FROM today) #最后活跃在近七天(T-6) AND MIN(DATE(in_time)) < DATE_SUB((SELECT * FROM today), INTERVAL 6 DAY) #非新晋用户:注册日期在近七天前 THEN '忠实用户' WHEN MIN(DATE(in_time)) BETWEEN DATE_SUB((SELECT * FROM today), INTERVAL 6 DAY) AND #新晋用户:首次登陆日期在近七天(T-6)之内 (SELECT * FROM today) THEN '新晋用户' WHEN MAX(DATE(in_time)) BETWEEN DATE_SUB((SELECT * FROM today), INTERVAL 29 DAY) AND #沉睡用户:最后活跃时间在7天之前(T-7),30天之内(T-29) DATE_SUB((SELECT * FROM today), INTERVAL 7 DAY) THEN '沉睡用户' ELSE '流失用户' END user_grade FROM tb_user_log GROUP BY 1 ) t GROUP BY 1 ORDER BY 2 DESC
Mysql 解法, 执行用时: 36ms, 内存消耗: 6512KB, 提交时间: 2021-12-14
WITH today AS( SELECT MAX(DATE(in_time)) FROM tb_user_log #今天的日期 ) SELECT user_grade, ROUND(COUNT(uid) / (SELECT COUNT(DISTINCT uid) FROM tb_user_log), 2) ratio FROM ( SELECT uid, CASE WHEN MAX(DATE(in_time)) BETWEEN DATE_SUB((SELECT * FROM today), INTERVAL 6 DAY) AND (SELECT * FROM today) #最后活跃在近七天(T-6) AND MIN(DATE(in_time)) < DATE_SUB((SELECT * FROM today), INTERVAL 6 DAY) #非新晋用户:注册日期在近七天前 THEN '忠实用户' WHEN MIN(DATE(in_time)) BETWEEN DATE_SUB((SELECT * FROM today), INTERVAL 6 DAY) AND (SELECT * FROM today) THEN '新晋用户' WHEN MAX(DATE(in_time)) BETWEEN DATE_SUB((SELECT * FROM today), INTERVAL 29 DAY) AND #沉睡用户:最后活跃时间在7天之前(T-7),30天之内(T-29) DATE_SUB((SELECT * FROM today), INTERVAL 7 DAY) THEN '沉睡用户' ELSE '流失用户' END user_grade FROM tb_user_log GROUP BY 1 ) t GROUP BY 1 ORDER BY 2 DESC
Mysql 解法, 执行用时: 36ms, 内存消耗: 6512KB, 提交时间: 2021-12-03
select user_grade, round(count(uid)/(select count(distinct uid) from tb_user_log),2) ratio from ( SELECT uid, case when (max(date(in_time)) BETWEEN '2021-10-29' and '2021-11-04') and (min(date(in_time)) <'2021-10-29' ) then '忠实用户' when min(date(in_time)) BETWEEN '2021-10-29' and '2021-11-04' then '新晋用户' when (max(date(in_time)) <= '2021-10-28') and (max(date(in_time)) >= '2021-10-06') then '沉睡用户' else '流失用户' end as user_grade FROM tb_user_log GROUP BY uid ) a GROUP BY user_grade ORDER BY ratio desc