我需要在过去 3 个月内循环查找活跃客户的数量。所以我进入我的交易表,找出在 3 个月内购买了东西的客户。但我想用解析函数来实现。
select add_months(dat.date,-3) as dateFrom
dat.date as dateTo,
(select count(distinct customerId)
from Trx_Table
where Trx_date between add_months(dat.date, -3) and sysdate
) as cnt
from dim_date dat
where dat.date between date '2017-01-01' and sysdate;
输出是这样的:
dateFrom dateTo cnt
20160930 20170101 10
20161001 20170102 12
20161002 20170103 14
这表明在 20170101 上,我们在 20160930 和 20170101(过去 3 个月)之间有 10 个活跃客户......等等。