如何计算 3 个月的活跃用户

How to calculate 3 month active users

我有一个 table,其中包含一个 orderId、一个时间戳和一个 customerId,如下所示:

DROP TABLE IF EXISTS testdata;

CREATE TABLE testdata (
`orderId` int,
`createdOn` datetime(6),
`customerId` int,
PRIMARY KEY (`orderId`)
);


INSERT INTO testdata (orderId, createdOn, customerId) VALUES

('1000001','2020-01-01 17:08:41.460000','101'),
('1000002','2020-01-02 18:01:00.180000','102'),
('1000003','2020-01-03 12:26:02.460000','103'),
('1000004','2020-01-04 13:32:42.610000','104'),
('1000005','2020-01-05 20:21:28.540000','101'),
('1000006','2020-01-06 11:54:20.530000','102'),

('1000007','2020-02-01 20:54:42.470000','102'),
('1000008','2020-02-02 10:21:29.470000','102'),
('1000009','2020-02-03 16:22:23.880000','102'),
('1000010','2020-02-04 16:22:23.880000','103'),
('1000011','2020-02-05 17:08:41.460000','103'),
('1000012','2020-02-06 18:01:00.180000','103'),

('1000013','2020-03-01 12:26:02.460000','102'),
('1000014','2020-03-02 13:32:42.610000','102'),
('1000015','2020-03-03 20:21:28.540000','103'),
('1000016','2020-03-04 11:54:20.530000','103'),
('1000017','2020-03-05 20:54:42.470000','104'),
('1000018','2020-03-06 10:21:29.470000','104'),

('1000019','2020-04-01 16:22:23.880000','103'),
('1000020','2020-04-02 16:22:23.880000','103'),
('1000021','2020-04-03 17:08:41.460000','103'),
('1000022','2020-04-04 18:01:00.180000','104'),
('1000023','2020-04-05 12:26:02.460000','104'),
('1000024','2020-04-06 13:32:42.610000','104'),

('1000025','2020-05-01 20:21:28.540000','103'),
('1000026','2020-05-02 11:54:20.530000','103'),
('1000027','2020-05-03 20:54:42.470000','104'),
('1000028','2020-05-04 10:21:29.470000','104'),
('1000029','2020-05-05 16:22:23.880000','105'),
('1000030','2020-05-06 16:22:23.880000','105'),

('1000031','2020-05-01 20:21:28.540000','104'),
('1000032','2020-05-02 11:54:20.530000','104'),
('1000033','2020-05-03 20:54:42.470000','104'),
('1000034','2020-05-04 10:21:29.470000','105'),
('1000035','2020-05-05 16:22:23.880000','105'),
('1000036','2020-05-06 16:22:23.880000','105')
;

现在我想计算每个月在过去 3 个月(即当月或前两个月)内活跃(即有订单)的客户数量。

我设法计算了当月的活跃用户,如下所示:

SELECT 
      EXTRACT(YEAR_MONTH FROM createdOn) AS order_createdOn_ym
      ,COUNT(DISTINCT customerId) AS mau
FROM testdata
GROUP BY order_createdOn_ym
ORDER BY order_createdOn_ym asc
;

(Fiddle 超过 here。)

但是,我完全不知道如何计算 3 个月活跃用户。

非常感谢任何帮助!

这是一种选择:

select c.createdmonth, count(distinct customerid) as mau
from (
    select distinct date_format(createdon, '%Y-%m-01') as createdmonth
    from testdata
) c
left join testdata t 
    on  t.createdon >= c.createdmonth - interval 2 month
    and t.createdon <  c.createdmonth + interval 1 month
group by c.createdmonth

想法是枚举不同的月份,然后将 table 与 left join 一起恢复最后 2 个月和当前月份。然后,您可以聚合并计算每组不同客户的数量。

感谢。纯粹出于个人喜好,我更喜欢按以下方式设置月份间隔:

SELECT date_format(c.end_of_createdOn_month, '%Y-%m') as order_month, 
       count(distinct customerid) as mau_3m
FROM (
    select distinct LAST_DAY(createdOn) as end_of_createdOn_month
    from testdata
) c
LEFT JOIN testdata t 
    ON  t.createdon >= (c.end_of_createdOn_month - interval 3 month)
    AND t.createdon <=  c.end_of_createdOn_month
GROUP BY c.end_of_createdOn_month;