Redshift SQL 以逗号分隔一个字段与一组两个字段
Redshift SQL to comma separate a field with group of two fields
我想用逗号分隔字段值和 GROUP BY
Redshift 中的其他两个字段。
示例数据:
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
但是 Redshift 不包含 STRING_AGG
函数。
我不知道如何在 Redshift 中执行此操作。
编辑
SELECT CUST_ID,
LISTAGG("ORDER", ', ')
WITHIN GROUP (ORDER BY "ORDER")
OVER (PARTITION BY CUST_ID) AS CUST_ID
FROM Table
ORDER BY CUST_ID
我的版本:
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: Specified types or functions (one per INFO message) not supported on Redshift tables.
我不明白,我只是从一个节点中选择?
我认为 Redshift 不支持 listagg()
作为 window 函数。所以,可以在单独聚合后的结果中join
:
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;
当然,我认为没有理由复制每一行的数据。聚合查询可能就足够了,因此结果集中每个客户只有一行。
我想用逗号分隔字段值和 GROUP BY
Redshift 中的其他两个字段。
示例数据:
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
但是 Redshift 不包含 STRING_AGG
函数。
我不知道如何在 Redshift 中执行此操作。
编辑
SELECT CUST_ID,
LISTAGG("ORDER", ', ')
WITHIN GROUP (ORDER BY "ORDER")
OVER (PARTITION BY CUST_ID) AS CUST_ID
FROM Table
ORDER BY CUST_ID
我的版本:
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: Specified types or functions (one per INFO message) not supported on Redshift tables.
我不明白,我只是从一个节点中选择?
我认为 Redshift 不支持 listagg()
作为 window 函数。所以,可以在单独聚合后的结果中join
:
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;
当然,我认为没有理由复制每一行的数据。聚合查询可能就足够了,因此结果集中每个客户只有一行。