我有一个包含以下信息的数据库
Customer_id, plan_id, plan_start_dte,
由于某些客户转换计划,有些客户有多个重复的customer_id
,但具有不同的plan_start_dte
。我试图计算一天成员从任何其他计划(plan_id = 'premium'
)切换到高级计划的次数。
也就是说,我正在尝试大致这样做:返回包含重复customer_id
的所有行,原始计划(min(plan_start_dte)
)除外,plan_id = 'premium'
除外,并将它们分组plan_start_dte
。
我能够获得所有重复记录及其计数:
with plan_counts as (
select c.*, count(*) over (partition by CUSTOMER_ID) ct
from CUSTOMERS c
)
select *
from plan_counts
where ct > 1
其他步骤让我陷入困境。首先,我尝试选择除原计划之外的所有内容:
SELECT CUSTOMERS c
where START_DTE not in (
select min(PLAN_START_DTE)
from CUSTOMERS i
where c.CUSTOMER_ID = i.CUSTOMER_ID
)
但这失败了。如果我能解决这个问题,我相信我必须添加的是c.PLAN_ID = 'premium'
的附加条件,然后按日期分组并进行计数。有人有什么想法吗?
答案 0 :(得分:0)
我想你想要lag()
:
select c.*
from (select c.*,
lag(plan_id) over (partition by customer_id order by plan_start_date) as prev_plan_id
from customers c
) c
where prev_plan_id <> 'premium' and plan_id = 'premium';
我不确定你想要什么输出。对于每天发生的次数:
select plan_start_date, count(*)
from (select c.*, lag(plan_id) over (partition by customer_id order by plan_start_date) as prev_plan_id
from customers c
) c
where prev_plan_id <> 'premium' and plan_id = 'premium'
group by plan_start_date
order by plan_start_date;