描述
用户行为日志表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 |
(uid-用户ID, artical_id-文章ID, in_time-进入时间, out_time-离开时间, sign_in-是否签到)
问题:统计活跃间隔对用户分级后,各活跃等级用户占比,结果保留两位小数,且按占比降序排序。
注:
- 用户等级标准简化为:忠实用户(近7天活跃过且非新晋用户)、新晋用户(近7天新增)、沉睡用户(近7天未活跃但更早前活跃过)、流失用户(近30天未活跃但更早前活跃过)。
- 假设今天就是数据中所有日期的最大值。
- 近7天表示包含当天T的近7天,即闭区间[T-6, T]。
输出示例:
示例数据的输出结果如下
user_grade | ratio |
---|---|
忠实用户 | 0.43 |
新晋用户 | 0.29 |
沉睡用户 | 0.14 |
流失用户 | 0.14 |
解释:
今天日期为2021.11.04,根据用户分级标准,用户行为日志表tb_user_log中忠实用户有:109、108、104;新晋用户有105、102;沉睡用户有103;流失用户有101;共7个用户,因此他们的比例分别为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
我的解题思路:
- 根据条件找出每个用户7天内、7-30天、30天以上的标记数
- 根据条件使用cast when语句分组
- 根据总人数和各分组人数找出比例
select user_grade,
round(count(1) / (select count(distinct uid) as cnt from tb_user_log), 2) as ratio
from (
select case
when (day7 > 0 and daymore > 0) or (day7 > 0 and day30 > 0)
then '忠实用户'
when day7 > 0 and daymore = 0 and day30 = 0
then '新晋用户'
when day30 > 0 and day7 = 0
then '沉睡用户'
else '流失用户'
end as user_grade
from (
select uid,
sum(case
when datediff(max_time, in_time) < 7 then 1
else 0 end) as day7,
sum(case
when datediff(max_time, in_time) >= 7 and datediff('2021-11-04', in_time) < 30 then 1
else 0 end) as day30,
sum(case
when datediff(max_time, in_time) >= 7 and datediff('2021-11-04', in_time) > 30 then 1
else 0 end) as daymore
from tb_user_log
left join (select max(date_format(in_time, '%Y-%m-%d')) as max_time from tb_user_log) t1
on 1 = 1
group by uid
) t
) tt
group by user_grade
order by ratio desc,
user_grade;
标签:SQL165,10,00,time,用户,牛客,2021,分级,11
From: https://www.cnblogs.com/moliyy/p/17663849.html