Sql 以红移方式将多行透视到列中
我正在使用aws redshift,有一个案例,我有多行用于唯一ID,需要使用SQL将行组合到每个唯一ID的一列中。我已经搜索了如何执行此操作,但在postgres中似乎是可能的,建议的方法使用的函数如:string_agg和array_agg在aws红移中不可用。这甚至可以使用红移吗?有人知道不使用函数就解决这个问题的方法吗 以下是我正在处理的问题。下表表示的查询为我提供了每个ID需要形成一列的行:Sql 以红移方式将多行透视到列中,sql,postgresql,select,amazon-web-services,amazon-redshift,Sql,Postgresql,Select,Amazon Web Services,Amazon Redshift,我正在使用aws redshift,有一个案例,我有多行用于唯一ID,需要使用SQL将行组合到每个唯一ID的一列中。我已经搜索了如何执行此操作,但在postgres中似乎是可能的,建议的方法使用的函数如:string_agg和array_agg在aws红移中不可用。这甚至可以使用红移吗?有人知道不使用函数就解决这个问题的方法吗 以下是我正在处理的问题。下表表示的查询为我提供了每个ID需要形成一列的行: +----+----------+---------+-------+ | ID | make
+----+----------+---------+-------+
| 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 | | | |
+----+----------+---------+-------+----------+--------+--------+
如果您的示例数据表示您的数据,并且每个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#的程序中启动一个简单的控制台应用程序,读取数据并写入新表。谢谢!!这真的很有帮助