gpt4 book ai didi

postgresql - Postgres 查询 : array_to_string with empty values

转载 作者:行者123 更新时间:2023-11-29 13:05:58 24 4
gpt4 key购买 nike

我正在尝试在 Postgres 查询中组合行并连接两列(名称、vorname)。

这样效果很好:

SELECT nummer, 
array_to_string(array_agg(name|| ', ' ||vorname), '\n') as name
FROM (
SELECT DISTINCT
nummer, name, vorname
FROM myTable
) AS m
GROUP BY nummer
ORDER BY nummer;

不幸的是,如果“vorname”为空,尽管 name 有值,但我没有得到任何结果。

是否有可能让它工作:

   array_to_string(array_agg(name|| ', ' ||vorname), '\n') as name

还有如果一列是空的?

最佳答案

使用 coalesce 将 NULL 值转换为可以连接的值:

array_to_string(array_agg(name|| ', ' ||coalesce(vorname, '<missing>')), '\n')

此外,您可以直接连接字符串,而无需使用 string_agg 函数将它们收集到数组中。

关于postgresql - Postgres 查询 : array_to_string with empty values,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12402640/

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