幸运的是,它适用于自11.2以来提供的listagg(…)函数(我们已经在运行),因此我们不必进一步调查:
listagg( abc,',' ) within group ( order by abc )
(正如人们应该知道的那样,wm_concat(…)是一些内部和官方不支持的功能.)
实现不同功能的rather nice solution(因为它不是那么臃肿)是通过自引用正则表达式功能实现的,这在许多情况下应该起作用:
regexp_replace(
listagg( abc,' ) within group ( order by abc ),'(^|,)(.+)(,\2)+','\1\2' )
(也许/希望我们将来会看到一些有效的listagg(不同的abc)功能,它会像wm_concat语法一样非常整洁和酷.例如,这是没有问题,因为很长一段时间与Postgres的string_agg(distinct abc)1)
-- 1: postgres sql example:
select string_agg( distinct x,' ) from unnest('{a,b,a}'::text[]) as x`
如果列表超过4000个字符,则不能再使用listagg(再次使用ORA-22922).
但幸运的是我们可以在这里使用xmlagg函数(如here所述).
如果你想在这里实现4000-chars截断结果的不同,你可以取消注释(1)标记的行.
-- in smallercase everything that could/should be special for your query
-- comment in (1) to realize a distinct on a 4000 chars truncated result
WITH cfg AS (
SELECT
',' AS list_delim,'([^,]+)(,\1)*(,|$)' AS list_dist_match,-- regexp match for distinct functionality
'\1\3' AS LIST_DIST_REPL -- regexp replace for distinct functionality
FROM DUAL
)
SELECT
--REGEXP_REPLACE( DBMS_LOB.SUBSTR( -- (1)
RTRIM( XMLAGG( XMLELEMENT( E,mycol,listdelim ).EXTRACT('//text()')
ORDER BY mycol ).GetClobVal(),LIST_DELIM )
--,4000 ),LIST_DIST_MATCH,LIST_DIST_REPL ) -- (1)
AS mylist
FROM mytab,CFG