2015-06-19 294 views
1

我正在使用aws红移,并且遇到了一个情况,那就是我有多个唯一ID的行,并且需要使用SQL将行组合到每个唯一ID的一列中。我搜索了如何做到这一点,它看起来像在postgres中是可能的,但是,建议的方法使用的功能如:string_agg和array_agg,这些功能在aws redshift中不可用。这甚至有可能使用红移?有没有人知道不使用函数来攻击这个问题的方法?在Redshift中将多行转换为列

以下是我正在处理的内容。下表代表给我,我需要组建成每个ID一列的行查询:

+----+----------+---------+-------+ 
| ID | make  | model | type | 
+----+----------+---------+-------+ 
| 1 | ford  | mustang | coupe | 
| 1 | toyota | celica | coupe | 
| 2 | delorean | btf  | coupe | 
| 2 | mini  | cooper | coupe | 
| 3 | ford  | mustang | coupe | 
| 3 |   |   |  | 
+----+----------+---------+-------+ 

什么我希望直到结束:

+----+----------+---------+-------+----------+---------+-------+ 
| ID | make  | model | type | make  | model | type | 
+----+----------+---------+-------+----------+---------+-------+ 
| 1 | ford  | mustang | coupe | toyota | celica | coupe | 
| 2 | delorean | bttf | coupe | mini  | cooper | coupe | 
| 3 | ford  | mustang | coupe |   |  |  | 
+----+----------+---------+-------+----------+--------+--------+ 

回答

1

如果你的样品数据表示你的数据,你只会有每个ID最多两个条目,那么你可以只加入到自身上而得到您所要求的输出:

select id, a.make, a.model, a.type, b.make, b.model, b.type 
from yourtable a 
    left outer join yourtable b 
    on b.id = a.id 
    and b.make != a.make 
    and b.model != a.model 
    and b.type != a.type 

如果有两个以上,你可以旋转了一个简单的控制台AP在类似c#的东西中,读入数据并写出一个新表。

+0

谢谢!这真的有帮助 – user1026498