建表
CREATE TABLE denglu(
id varchar(10),
dt date)
插入测试数据
INSERT INTO denglu
VALUES
('01','2021-10-01'),
('01','2021-10-01'),
('02','2021-10-03'),
('02','2021-10-02'),
('03','2021-10-01'),
('03','2021-10-04'),
('04','2021-10-06'),
('05','2021-10-07'),
('06','2021-10-06'),
('06','2021-10-05'),
('06','2021-10-04');
Q1 每日活跃用户数
按天数统计,需要group by dt,一个用户一天内可能多次登陆,需要distinct去重,语法如下
SELECT dt,count(DISTINCT id) FROM denglu
GROUP BY dt ORDER BY dt
Q2 每日新增用户数
该日新增用户,即在此日期之前没有出现过的用户,因此每个用户的最小登陆日期计为那一日的新增,语法如下
SELECT dt2,count(DISTINCT id) FROM (
SELECT id,min(dt)AS dt2 FROM denglu GROUP BY id
)
GROUP BY dt2
Q3 每日累计用户数
先放语法
SELECT dt2,count(DISTINCT CASE WHEN dt1<=dt2 THEN id end) FROM (
(SELECT distinct dt AS dt1,id FROM denglu) a
LEFT JOIN (SELECT DISTINCT dt AS dt2 FROM denglu)b ON 1=1
)
GROUP BY dt2
拆解,先找出每日登陆用户
SELECT distinct dt AS dt1,id FROM denglu
DT1 | ID |
---|---|
2021-10-01 | 03 |
2021-10-01 | 01 |
2021-10-02 | 02 |
2021-10-03 | 02 |
2021-10-04 | 03 |
2021-10-04 | 06 |
2021-10-05 | 06 |
2021-10-06 | 04 |
2021-10-06 | 06 |
2021-10-07 | 05 |
第二步,左关联一个distinct去重的日期,方便后面统计每日累计
SELECT * FROM (
(SELECT distinct dt AS dt1,id FROM denglu) a
LEFT JOIN (SELECT DISTINCT dt AS dt2 FROM denglu)b ON 1=1
)ORDER BY DT1
FETCH FIRST 20 ROWS ONLY;
DT1 | ID | DT2 |
---|---|---|
2021-10-01 | 01 | 2021-10-01 |
2021-10-01 | 01 | 2021-10-02 |
2021-10-01 | 01 | 2021-10-03 |
2021-10-01 | 01 | 2021-10-04 |
2021-10-01 | 01 | 2021-10-05 |
2021-10-01 | 01 | 2021-10-06 |
2021-10-01 | 01 | 2021-10-07 |
2021-10-01 | 03 | 2021-10-01 |
2021-10-01 | 03 | 2021-10-02 |
2021-10-01 | 03 | 2021-10-03 |
2021-10-01 | 03 | 2021-10-04 |
2021-10-01 | 03 | 2021-10-05 |
2021-10-01 | 03 | 2021-10-06 |
2021-10-01 | 03 | 2021-10-07 |
2021-10-02 | 02 | 2021-10-01 |
2021-10-02 | 02 | 2021-10-02 |
2021-10-02 | 02 | 2021-10-03 |
2021-10-02 | 02 | 2021-10-04 |
2021-10-02 | 02 | 2021-10-05 |
2021-10-02 | 02 | 2021-10-06 |
之后就以DT2为每日统计,按天数统计,group by DT2,当DT1<=DT2,则计为DT2累计用户