gpt4 book ai didi

sql - 如何在 Amazon Redshift 中组合行

转载 作者:行者123 更新时间:2023-12-04 22:23:52 24 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Can I concatenate multiple MySQL rows into one field?

(14 个回答)


3年前关闭。




我正在使用亚马逊 Redshift 。
如何组合列的结果。

如果原始行是:

*ID   Name  Color
----------------
1 John White
1 John Black
2 Mark Blue
2 Mark Red*

结果应该是:
*ID   Name  Color
----------------
1 John White Black
2 Mark Blue Red*

最佳答案

Redshift 提供了一个函数 LISTAGG() 来满足你的需要

SELECT id, name, LISTAGG(Color,' ') AS Colors
FROM yourtable
GROUP BY id, name

For each group in a query, the LISTAGG aggregate function orders the rows for that group according to the ORDER BY expression, then concatenates the values into a single string. http://docs.aws.amazon.com/redshift/latest/dg/r_LISTAGG.html


SELECT id, name
, LISTAGG(Color,' ') WITHIN GROUP (ORDER BY name) AS Colors
FROM yourtable
GROUP BY id, name

关于sql - 如何在 Amazon Redshift 中组合行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47195663/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com