Redshift SQL 用 GROUP 逗号分隔字段

Posted

技术标签:

【中文标题】Redshift SQL 用 GROUP 逗号分隔字段【英文标题】:Redshift SQL to comma separate a field with GROUP 【发布时间】:2019-12-10 09:59:53 【问题描述】:

我想用逗号分隔 Redshift 中的字段值和 GROUP BY 其他两个字段。

样本数据:

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 中执行此操作。

编辑

Using the answer from here does NOT WORK:

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:Redshift 表不支持指定的类型或函数(每条 INFO 消息一个)。

我不明白,因为我只从单个节点中选择?

【问题讨论】:

【参考方案1】:
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;

【讨论】:

以上是关于Redshift SQL 用 GROUP 逗号分隔字段的主要内容,如果未能解决你的问题,请参考以下文章

sql server 2008 group_Concat() 版本但是在不同的列中(不是逗号分隔)

Redshift :- 易于将逗号分隔的字符串拆分为行

匹配 Redshift 中逗号分隔字段中的值

SQL:将行与列中的逗号分隔值合并

从逗号分隔的字符串中删除重复项 (Amazon Redshift)

将带逗号的双引号作为分隔符从 S3 导入 Amazon Redshift