假设我有这样的数据
create table example
(cust_id VARCHAR, product VARCHAR, price float, datetime varchar);
insert into example (cust_id, product, price, datetime)
VALUES
('1', 'scooter', 2000, '2022-01-10'),
('1', 'skateboard', 1500, '2022-01-20'),
('1', 'beefmeat', 300, '2022-06-08'),
('2', 'wallet', 200, '2022-02-25'),
('2', 'hairdryer', 250, '2022-04-28'),
('3', 'skateboard', 1600, '2022-03-29')
我想要添加一些行,然后根据这些添加的行创建新的列
我的预期yields 会是这样的
cust_id | total_price | date | is_active |
---|---|---|---|
1 | 3500 | 2022-01 | active |
1 | 0 | 2022-02 | active |
1 | 0 | 2022-03 | active |
1 | 0 | 2022-04 | inactive |
1 | 0 | 2022-05 | inactive |
1 | 300 | 2022-06 | active |
1 | 0 | 2022-07 | active |
2 | 0 | 2022-01 | inactive |
2 | 200 | 2022-02 | active |
2 | 0 | 2022-03 | active |
2 | 250 | 2022-04 | active |
2 | 0 | 2022-05 | active |
2 | 0 | 2022-06 | active |
2 | 0 | 2022-07 | inactive |
3 | 0 | 2022-01 | inactive |
3 | 0 | 2022-02 | inactive |
3 | 1600 | 2022-03 | active |
3 | 0 | 2022-04 | active |
3 | 0 | 2022-05 | active |
3 | 0 | 2022-06 | inactive |
3 | 0 | 2022-07 | inactive |
规则是这样的
- the first month when the customer make transaction is called active, before this transaction called inactive.
ex: first transaction in month 2, then month 2 is active, month 1 is inactive (look cust_id 2 and 3) - if more than 2 months there isnt transaction, the next month is inactive until there is new transaction is active.
ex: if last transaction in month 1, then month 2 and month 3 is inactive, and month 4, month 5 inactive if month 6 there is new transaction (look cust_id 1 and 3)
嗯,我的第一个 idea 是使用这个代码,但我不知道下一步是什么
select *,
date_part('month', age(to_date(date, 'YYYY-MM'), to_date(lag(date) over (partition by cust_id order by date),'YYYY-MM')))date_diff
from(
select
cust_id,
sum(price)total_price,
to_char(to_date(datetime, 'YYYY-MM-DD'),'YYYY-MM')date
from example
group BY
cust_id,
date
order by
cust_id,
date)test
我愿意接受任何建议