[英]Redshift - Splitting data into multiple rows
I have sales data that shows customers that have bought a product. 我有销售数据,显示已购买产品的客户。 All customer IDs are appended to the same cell as shown below. 如下所示,所有客户ID都附加到同一单元格中。
How could I split this such that I have one row per cust_id per prod_id 我该如何拆分,以便每个prod_id每个cust_id有一行
prod_id,cust_id
10001,100,101
10002,102
Expected output: 预期产量:
prod_id,cust_id
10001,100
10001,101
10002,102
Since Redshift supports generate_series
now, you can use the following SQL to achieve the desired result 由于Redshift现在支持generate_series
,因此您可以使用以下SQL来获得所需的结果
with test(prod_id,cust_id) as (
select 10001,'100,101' union all
select 10002,'102'
),
max_ids as (
select distinct generate_series(1, regexp_count(cust_id,',') +1) n from test
)
select distinct prod_id, split_part(cust_id, ',', n) cust_id
from test, max_ids
where split_part(cust_id, ',', n) != ''
output is 输出是
prod_id cust_id
10001 100
10001 101
10002 102
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.