Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
红移SQL以逗号分隔带组的字段_Sql_Amazon Redshift_String Aggregation - Fatal编程技术网

红移SQL以逗号分隔带组的字段

红移SQL以逗号分隔带组的字段,sql,amazon-redshift,string-aggregation,Sql,Amazon Redshift,String Aggregation,我想用逗号分隔一个字段的值,并在红移中按分组另外两个字段 样本数据: table_schema table_name column_name G1 G2 a G1 G2 b G1 G2 c G1 G2 d G3 G4 x G3 G4 y G3 G4

我想用逗号分隔一个字段的值,并在红移中按分组另外两个字段

样本数据:

table_schema table_name column_name 
G1           G2         a
G1           G2         b
G1           G2         c
G1           G2         d
G3           G4         x
G3           G4         y
G3           G4         z
预期产出:

table_schema table_name column_name 
G1           G2         a, b, c, d
G3           G4         x, y, z
我可以在MSSQL中这样做:

SELECT table_schema, table_name, column_name = 
    STUFF((SELECT ', ' + column_name
           FROM your_table b 
           WHERE b.table_schema = a.table_schema AND b.table_name = a.table_name
          FOR XML PATH('')), 1, 2, '')
FROM information_schema.tables t
INNER JOIN information_schema.columns c on c.table_name = t.table_name AND c.table_schema = t.table_schema
GROUP BY table_schema, table_name
在PostgreSQL中,这将是:

SELECT table_schema, table_name, String_agg(column_name, ',')
FROM information_schema.tables t
INNER JOIN information_schema.columns c on c.table_name = t.table_name AND c.table_schema = t.table_schema
GROUP BY table_schema, table_name
但是红移不包含
STRING\u AGG
函数

我不知道如何在红移中做到这一点

编辑

我的版本:

SELECT t.table_name, LISTAGG("column_name", ', ')
WITHIN GROUP (ORDER BY "column_name")
OVER (PARTITION BY t.table_name) AS table_schema
FROM information_schema.columns t
ORDER BY t.table_name
它给了我以下错误:

0A000:红移表不支持指定的类型或功能(每个信息消息一个)

我不明白,因为我只是从一个节点中选择

SELECT t.CUST_ID, c.orders
FROM Table t JOIN
      (SELECT cust_id, LISTAGG("ORDER"::text, ', ')
WITHIN GROUP (ORDER BY "ORDER") as orders
       FROM table t
       GROUP BY cust_id
      ) c
      ON t.cust_id = c.cust_id
ORDER BY CUST_ID;
SELECT t.CUST_ID, c.orders
FROM Table t JOIN
      (SELECT cust_id, LISTAGG("ORDER"::text, ', ')
WITHIN GROUP (ORDER BY "ORDER") as orders
       FROM table t
       GROUP BY cust_id
      ) c
      ON t.cust_id = c.cust_id
ORDER BY CUST_ID;