Vertica:将字符串拆分为数组并将其分组以获取一组唯一值?

时间:2017-12-17 17:20:02

标签: sql vertica

我在Vertica上有一个专栏,它将数字存储为逗号连接的值字符串,例如:12,15,17,例如:

ID  |  values
----------------------
A   | 1,3,5
A   | 2,3,5,6
B   | 1,2,3

我想把这作为我的最终结果:

ID  |  values
----------------------
A   | 1,2,3,5,6
B   | 1,2,3  

意思是,每个ID都包含所有不同的值。我该怎么做呢? (使用Vertica 8.1.1)

1 个答案:

答案 0 :(得分:0)

找到它: 假设您知道每个条目的最大值是多少(在本例中假设为6):

with m as (
  select 1 as i union all
  select 2 as i union all
  select 3 as i union all
  select 4 as i union all
  select 5 as i union all
  select 6 as i),
splitted_data as (
  select distinct ID, split_part('values',',',i) as value
  from main_table cross join m)
select ID, rtrim(agg_concatenate(value||','),',') as values
from splitted_data