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

Snowflake cloud data platform 横向展平雪花中具有不同阵列长度的两列,snowflake-cloud-data-platform,Snowflake Cloud Data Platform,我是新的雪花,目前正在学习使用横向平坦 我目前有一个虚拟表,如下所示: 用于“客户号”和“城市”的数据类型为数组 我已设法理解并应用扁平化概念,使用以下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, latera

我是新的雪花,目前正在学习使用横向平坦

我目前有一个虚拟表,如下所示:

用于“客户号”和“城市”的数据类型为数组

我已设法理解并应用扁平化概念,使用以下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;
显示的输出为:

我们可以从虚拟表中清楚地看到,在第4行中,customer_id 104有3个数字,我希望在我的输出中看到这三个数字,如果城市中没有匹配的索引值,我希望在“City”中看到“Null”

我的预期产出是:
这有可能做到吗

您可能希望为此任务使用
左侧外部联接
,但需要先创建城市的行集版本

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;

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

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

  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;

非常感谢,太棒了,现在我明白了轻松实现所需输出的方法。这确实是您提出的一个非常有效的解决方案。您好,这与我之前尝试的方法类似,但不幸的是,它提示我一条错误消息:“函数'SPLIT'的参数类型无效:(ARRAY,VARCHAR(1))”。SPLIT是您在variant上使用的一种方法,不在阵列上-根据错误消息,这是您尝试执行的操作。你在那里使用的是什么数据类型?哦,我知道了,我在“城市”中使用的数据类型是数组。这就是为什么我会收到那个错误信息。