【问题标题】:Redshift - Splitting data into multiple rowsRedshift - 将数据拆分为多行
【发布时间】:2019-09-04 12:26:33
【问题描述】:

我有销售数据显示购买了产品的客户。所有客户 ID 都附加到同一个单元格,如下所示。

我如何拆分它,以便每个 cust_id 每个 prod_id 有一行

prod_id,cust_id
10001,100,101
10002,102

预期输出:

prod_id,cust_id
10001,100
10001,101
10002,102

【问题讨论】:

标签: sql amazon-redshift


【解决方案1】:

由于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) != ''

输出是

prod_id cust_id
10001   100
10001   101
10002   102

【讨论】:

  • 哦,终于!多么大的突破(RE 支持 generate_series)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多