如何标记每月至少一项交易的活跃客户



目标是为活跃客户创建标志。活跃的客户是每个月至少一次交易的人。

时间范围 - 2018年5月至2019年5月数据处于交易级别

-------------------------------------
txn_id | txn_date | name | amount
-------------------------------------
101     2018-05-01  ABC    100
102     2018-05-02  ABC    200
-------------------------------------

output should be like this -
----------------
name | flag
----------------
ABC    active
BCF    inactive

您可以使用聚合来获取活跃的客户:

select name
from t
where txn_date >= '2018-05-01' and txn_date < '2019-06-01'
group by name
having count(distinct last_day(txn_date)) = 13  -- all months accounted for

编辑:

如果您想要标志,只需将条件移至case表达式:

select name,
       (case when count(distinct case when txn_date >= '2018-05-01' and txn_date < '2019-06-01' then last_day(txn_date) end) = 13
             then 'active' else 'inactive'
        end) as flag
from t;

最新更新