查询每个客户具有多行(销售漏斗)事件的规范化表

oknrviil  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(574)

我有一个销售漏斗数据集,每一行代表通过漏斗的移动。由于潜在客户有很多方法可以通过漏斗移动(并向后移动),所以我不打算对表进行扁平化/非规范化。例如,我如何计算“有多少客户流失和重新激活”?

customer | opp_id | status_old | status_new | current_opp_status | status_change_date
cust_8   | 22     | confirmed  | paying     | paying             | 2020-01-01
cust_9   | 23     | confirmed  | paying     | churned            | 2020-01-03
cust_9   | 23     | paying     | churned    | churned            | 2020-01-08
cust_12  | 24     | confirmed  | paying     | churned            | 2020-01-01
cust_12  | 24     | paying     | churned    | churned            | 2020-03-21
cust_12  | 28     | confirmed  | paying     | paying             | 2020-04-03
cust_13  | 30     | confirmed  | paying     | paying             | 2020-02-03
...      | ...    | ...        | ...        | ...                | ...

在上面的例子中,我想从本质上确定custèu12,因为他们进行了翻腾,但后来又以付费客户的身份回来了,有了一个新的opportunity id,但我甚至还在为从哪里开始而挣扎。有什么建议吗?

r3i60tvu

r3i60tvu1#

我认为可以使用聚合:

select customer
from t
group by customer
having min(case when status_new = 'churned' then status_change_date end) <
       max(case when status_new = 'paying' then status_change_date end);

实际上,根据你的数据 where 可能就足够了:
从t中选择t.*,其中status\u new='paying'和status\u old='chorred'

相关问题