在Redshift

时间:2015-06-19 14:18:46

标签: sql postgresql select amazon-web-services amazon-redshift

我正在使用aws redshift并且有一个案例,我有一个唯一ID的行,需要使用SQL将行组合成每个唯一ID的一列。我已经搜索了如何执行此操作,看起来它可能在postgres中,但是,建议的方法使用的函数如:string_agg和array_agg在aws 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 个答案:

答案 0 :(得分: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

如果你有两个以上,你可以在c#之类的东西中启动一个简单的控制台应用程序,读入数据并写出新表。

相关问题