目标是为活跃客户创建一个标志。活跃客户是指每个月至少有一笔交易的人。
时间范围- 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发布于 2019-07-03 18:34:48
您可以使用聚合来获取活跃客户:
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;https://stackoverflow.com/questions/56867955
复制相似问题