Why does the wm_concat not work here?(为什么 wm_concat 在这里不起作用?)
问题描述
我有这个查询:
(SELECT OBJECT_ID from cr_object_group_entries_vw where object_group_id IN
(SELECT ITEM FROM TABLE(CR_FN_SPLIT_STRING('28,56',','))))
返回:
但是当我这样做时:
SELECT wm_concat(object_id) FROM
(SELECT OBJECT_ID from cr_object_group_entries_vw where object_group_id IN
(SELECT ITEM FROM TABLE(CR_FN_SPLIT_STRING('28,56',','))))
我得到一个空白结果......我做错了什么?
I get a blank result... what am I doing wrong?
推荐答案
你必须避免使用 wm_concat
函数,因为它没有被记录并且在 Oracle 8i 时代被发现作为解决方法.
You must avoid wm_concat
function because it is undocumented and discovered as workaround at Oracle 8i times.
自从 Tom Kyte 发现具有自定义聚合函数的旧方法以来 此处 有一些新的解决方法,如下面的示例所示.
Since times of old method with custom aggregate function as discovered by Tom Kyte here there are some new workarounds, showed at examples below.
所有这些都在这个 SQL Fiddle 中复制.
All of them reproduced in this SQL Fiddle.
解决方法 1 - LISTAGG 函数,适用于 11g:
Workaround 1 - LISTAGG function, works in 11g:
select listagg(object_id,',') within group (order by rownum) id_string
from cr_object_group_entries_vw
解决方法 2 - SYS_CONNECT_BY_PATH,从 10g 开始工作:
Workaround 2 - SYS_CONNECT_BY_PATH, works since 10g:
select id_string from (
select rn, substr(sys_connect_by_path(object_id, ','),2) id_string
from (select object_id, rownum rn from cr_object_group_entries_vw)
start with rn = 1
connect by prior rn + 1 = rn
order by rn desc
)
where rownum = 1
解决方法 3 - XMLAGG,从 10g 开始工作:
Workaround 3 - XMLAGG, works since 10g:
select replace(
replace(
replace(
xmlagg(xmlelement("x",object_id)).getStringVal(),
'</x><x>',
','
),
'<x>',
''
),
'</x>',
''
) id_string
from cr_object_group_entries_vw
附言我不知道在哪个 Oracle 版本 sys_connect_by_path
和 xmlagg
中被引入,但两者都在 10.2.0.4.0 上运行良好
P.S. I didn't know exactly in which Oracle versions sys_connect_by_path
and xmlagg
was introduced, but both works well on 10.2.0.4.0
这篇关于为什么 wm_concat 在这里不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么 wm_concat 在这里不起作用?


基础教程推荐
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01