相当于 Oracle 9i 中的 PostgreSQL array() / array_to_string() 函数
我希望从在 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;
Will return:
names
-------------
bob,alice,jon
如何在 Oracle 9i 中实现相同的结果?
谢谢,
马特
I'm hoping to return a single row with a comma separated list of values from a query that returns multiple rows in Oracle, essentially flattening the returned rows into a single row.
In PostgreSQL this can be achieved using the array and array_to_string functions like this:
Given the table "people":
id | name
---------
1 | bob
2 | alice
3 | jon
The SQL:
select array_to_string(array(select name from people), ',') as names;
Will return:
names
-------------
bob,alice,jon
How would I achieve the same result in Oracle 9i?
Thanks,
Matt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Tim Hall 拥有Oracle 中的字符串聚合技术的权威合集。
如果您被困在 9i 上,我个人的偏好是定义一个自定义聚合(该页面上有 string_agg 的实现),这样您就可以
但必须定义一个新的 STRING_AGG 函数。如果您需要避免创建新对象,还有其他方法,但在 9i 中它们将比 PostgreSQL 语法更混乱。
Tim Hall has the definitive collection of string aggregation techniques in Oracle.
If you're stuck on 9i, my personal preference would be to define a custom aggregate (there is an implementation of string_agg on that page) such that you would have
But you have to define a new STRING_AGG function. If you need to avoid creating new objects, there are other approaches but in 9i they're going to be messier than the PostgreSQL syntax.
在 10g 中,我绝对更喜欢 Tim 文章末尾提到的 COLLECT 选项。
这种方法的好处是,相同的底层函数(接受集合作为参数)既可以用作聚合函数,也可以用作多重集函数:
但是在 9i 中这是不可用的。 SYS_CONNECT_BY_PATH 很好,因为它很灵活,但它可能很慢,所以要小心。
In 10g I definitely prefer the COLLECT option mentioned at the end of Tim's article.
The nice thing about that approach is that the same underlying function (that accepts the collection as an argument), can be used both as an aggregate and as a multiset function:
However in 9i that's not available. SYS_CONNECT_BY_PATH is nice because it's flexible, but it can be slow, so be careful of that.