Window 雪花中的函数
Window function in Snowflake
我的数据结构如下 -
1.For每个ID月表示报告月,Sub created是原始订阅购买日期,status =客户是否活跃,tenure是lifetime months(客户返回时重置为1)
ID Month Sub_created status tenure
100 2017-02-01 2017-02-01 active 1
100 2017-03-01 active 2
100 2017-04-01 active 3
100 2017-05-01 churned 3
100 2021-02-01 2021-02-01 active 1
100 2021-03-01 active 2
100 2021-04-01 active 3
100 2021-05-01 active 4
100 2021-06-01 active 5
100 2021-07-01 active 6
我希望能够为所有行创建子项,直到它有一个新的订阅日期。我试图获得的输出低于 -
ID Month Sub_created status tenure
100 2017-02-01 2017-02-01 active 1
100 2017-03-01 2017-02-01 active 2
100 2017-04-01 2017-02-01 active 3
100 2017-05-01 2017-02-01 churned 3
100 2021-02-01 2021-02-01 active 1
100 2021-03-01 2021-02-01 active 2
100 2021-04-01 2021-02-01 active 3
100 2021-05-01 2021-02-01 active 4
100 2021-06-01 2021-02-01 active 5
100 2021-07-01 2021-02-01 active 6
谁能推荐雪花代码?谢谢
您可以像这样使用 last_value() window 函数:
with CTE as (
select 100 as ID, '2017-02-01' as Month, '2017-02-01' as Sub_created, 'active' as status, 1 as tenure union all
select 100 as ID, '2017-03-01' as Month, null as Sub_created, 'active' as status, 2 as tenure union all
select 100 as ID, '2017-04-01' as Month, null as Sub_created, 'active' as status, 3 as tenure union all
select 100 as ID, '2017-05-01' as Month, null as Sub_created, 'churned' as status, 3 as tenure union all
select 100 as ID, '2021-02-01' as Month, '2021-02-01' as Sub_created, 'active' as status, 1 as tenure union all
select 100 as ID, '2021-03-01' as Month, null as Sub_created, 'active' as status, 2 as tenure union all
select 100 as ID, '2021-04-01' as Month, null as Sub_created, 'active' as status, 3 as tenure union all
select 100 as ID, '2021-05-01' as Month, null as Sub_created, 'active' as status, 4 as tenure union all
select 100 as ID, '2021-06-01' as Month, null as Sub_created, 'active' as status, 5 as tenure union all
select 100 as ID, '2021-07-01' as Month, null as Sub_created, 'active' as status, 6 as tenure
)
select ID, Month, Sub_created as Sub_created_orig,
last_value(sub_created ignore nulls) over (partition by id order by month rows between unbounded preceding and current row) as Sub_created_new,
status, tenure
from CTE
order by ID, month;
我的数据结构如下 -
1.For每个ID月表示报告月,Sub created是原始订阅购买日期,status =客户是否活跃,tenure是lifetime months(客户返回时重置为1)
ID Month Sub_created status tenure
100 2017-02-01 2017-02-01 active 1
100 2017-03-01 active 2
100 2017-04-01 active 3
100 2017-05-01 churned 3
100 2021-02-01 2021-02-01 active 1
100 2021-03-01 active 2
100 2021-04-01 active 3
100 2021-05-01 active 4
100 2021-06-01 active 5
100 2021-07-01 active 6
我希望能够为所有行创建子项,直到它有一个新的订阅日期。我试图获得的输出低于 -
ID Month Sub_created status tenure
100 2017-02-01 2017-02-01 active 1
100 2017-03-01 2017-02-01 active 2
100 2017-04-01 2017-02-01 active 3
100 2017-05-01 2017-02-01 churned 3
100 2021-02-01 2021-02-01 active 1
100 2021-03-01 2021-02-01 active 2
100 2021-04-01 2021-02-01 active 3
100 2021-05-01 2021-02-01 active 4
100 2021-06-01 2021-02-01 active 5
100 2021-07-01 2021-02-01 active 6
谁能推荐雪花代码?谢谢
您可以像这样使用 last_value() window 函数:
with CTE as (
select 100 as ID, '2017-02-01' as Month, '2017-02-01' as Sub_created, 'active' as status, 1 as tenure union all
select 100 as ID, '2017-03-01' as Month, null as Sub_created, 'active' as status, 2 as tenure union all
select 100 as ID, '2017-04-01' as Month, null as Sub_created, 'active' as status, 3 as tenure union all
select 100 as ID, '2017-05-01' as Month, null as Sub_created, 'churned' as status, 3 as tenure union all
select 100 as ID, '2021-02-01' as Month, '2021-02-01' as Sub_created, 'active' as status, 1 as tenure union all
select 100 as ID, '2021-03-01' as Month, null as Sub_created, 'active' as status, 2 as tenure union all
select 100 as ID, '2021-04-01' as Month, null as Sub_created, 'active' as status, 3 as tenure union all
select 100 as ID, '2021-05-01' as Month, null as Sub_created, 'active' as status, 4 as tenure union all
select 100 as ID, '2021-06-01' as Month, null as Sub_created, 'active' as status, 5 as tenure union all
select 100 as ID, '2021-07-01' as Month, null as Sub_created, 'active' as status, 6 as tenure
)
select ID, Month, Sub_created as Sub_created_orig,
last_value(sub_created ignore nulls) over (partition by id order by month rows between unbounded preceding and current row) as Sub_created_new,
status, tenure
from CTE
order by ID, month;