减去雪花/DBT 中的列
Posted
技术标签:
【中文标题】减去雪花/DBT 中的列【英文标题】:Subtracting columns in snowflake/DBT 【发布时间】:2021-09-22 12:21:27 【问题描述】:我有两列,一列是挑选对象,另一列是每个客户的未挑选对象。
我需要创建一个包含最终对象的列,例如:
Customer_ID | Picked. | Unpicked. | . Final Cart |
---|---|---|---|
799235 | shirt, pants, glasses | . glasses. | shirt, pants |
799246 | dress, pants, glasses | . pants, dresses | glasses |
我试图在 Snowflake 中执行此操作,但看起来定义的函数都不起作用。
客户可以根据需要多次添加和删除项目,我只对最终值感兴趣。
【问题讨论】:
【参考方案1】:不幸的是,dbt jinja 对这个问题没有用处,因为它(几乎总是)在 SQL 运行时之前编译,所以它无法访问这些值。
这个可以,但是可以用 Snowflake SQL 解决。基本模式是将值展平,然后进行减法,然后将它们聚合在一起。
这里有一些代码:
with carts as (
select 799235 as customer_id, array_construct('shirt', 'pants', 'glasses') as picked, array_construct('glasses') as unpicked
union
select 799246, array_construct('dress', 'pants', 'glasses'), array_construct('pants', 'dress')
),
picked_expanded as (
select
carts.customer_id,
picked.value::varchar as item
from carts, lateral flatten ( input => carts.picked ) as picked
),
unpicked_expanded as (
select
carts.customer_id,
unpicked.value::varchar as item
from carts, lateral flatten ( input => carts.unpicked ) as unpicked
),
combined as (
select * from picked_expanded
except
select * from unpicked_expanded
),
agg as (
select
customer_id,
array_agg(item) as final_cart
from combined
group by 1
)
select * from agg
结果如下:
CUSTOMER_ID | FINAL_CART |
---|---|
799246 | ["glasses"] |
799235 | ["shirt", "pants"] |
【讨论】:
以上是关于减去雪花/DBT 中的列的主要内容,如果未能解决你的问题,请参考以下文章
Avro 列中的 NULL 值加载为 VARIANT NULL