gpt4 book ai didi

arrays - 等同于 Oracle 9i 中的 PostgreSQL array()/array_to_string() 函数

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

我希望从在 Oracle 中返回多行的查询中返回包含逗号分隔值列表的单行,实质上是将返回的行展平为单行。

在 PostgreSQL 中,这可以使用 array 和 array_to_string 函数来实现,如下所示:

给定表“people”:

id | name
---------
1 | bob
2 | alice
3 | jon

SQL:

select array_to_string(array(select name from people), ',') as names;

将返回:

names
-------------
bob,alice,jon

如何在 Oracle 9i 中获得相同的结果?

谢谢,

马特

最佳答案

Tim Hall 拥有权威收藏 string aggregation techniques in Oracle .

如果您坚持使用 9i,我个人的偏好是定义一个自定义聚合(该页面上有一个 string_agg 的实现)这样您就可以拥有

SELECT string_agg( name )
FROM people

但是您必须定义一个新的 STRING_AGG 函数。如果您需要避免创建新对象,还有其他方法,但在 9i 中它们将比 PostgreSQL 语法更困惑。

关于arrays - 等同于 Oracle 9i 中的 PostgreSQL array()/array_to_string() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4326868/

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