横向展平雪花中具有不同阵列长度的两列

Lateral Flatten two columns with different array length in snowflake

我是 snowflake 的新手,目前正在学习使用 Lateral Flatten。

我目前有一个虚拟 table 看起来像这样:

"Customer_Number" & "Cities" 使用的数据类型是数组。

我已经设法理解并应用 Flatten 概念,使用以下 sql 语句展开数据:

select c.customer_id, c.last_name, f.value as cust_num, f1.value as city
    from customers as c,
    lateral flatten(input => c.customer_number) f,
    lateral flatten(input => c.cities) f1
    where f.index = f1.index
    order by customer_id;

显示的输出是:

我们可以从虚拟 table 中清楚地看到,在第 4 行 customer_id 104 有 3 个数字,我希望在输出中看到所有这三个数字,如果没有匹配的话我只想在 "City".

中看到 "Null" 中的城市指数值

我的预期输出是: 这可能完成吗?

您可能希望使用 LEFT OUTER JOIN 来完成此任务,但需要先创建城市的行集版本。

select c.customer_id, c.last_name, f.value as cust_num, f1.value as city
    from customers as c
    cross join lateral flatten(input => c.customer_number) f
    left outer join (select * from customers, lateral flatten(input => cities)) f1
                 on f.index = f1.index
    order by customer_id;

只要你能确定第二条记录会更短,你可以这样做:

select customer_id, last_name, list1_table.value::varchar as customer_number, 
split(cities,',')[list1_table.index]::varchar as city
from customers, lateral flatten(input=>split(customer_number, ',')) list1_table;

否则,您必须在 2 组记录之间执行 union(常规 union 将消除重复项)

诀窍是删除第二个横向,并使用第一个的索引从第二个数组中选择值:

  select c.customer_id, c.last_name, f.value as cust_num, cites[f.index] as city
    from customers as c,
    lateral flatten(input => c.customer_number) f
    order by customer_id;