【发布时间】:2019-11-29 20:58:44
【问题描述】:
我试图使用 LISTAGG 和 REGEXP_REPLACE 函数连接不同的行值,如下面的链接所述: LISTAGG in Oracle to return distinct values。 试图了解发生了什么,我做了一些改变并开始使用它。然而,我没有得到我期待的答案。似乎我缺少一些关于正则表达式引擎如何处理“空字符串”和“空格”的关键概念。有人可以逐步介绍一下正则表达式引擎如何处理以下示例查询中的 why_space_affected_here、why_space_affected_here、why_space_not_affected_here、matching_empty_string 和 why_space_not_affected 列?
select group_key
, listagg(id, ', ') within group (order by id) as listagg_output`
, regexp_replace(listagg(id, ', ') within group (order by id), '([^,]*)(, \1)+', '\1') as why_space_affected_here
, regexp_replace(listagg(id, ', ') within group (order by id), '([^,]*)(,\1)+', '\1') as why_space_not_affected_here
, regexp_replace(listagg(id, ', ') within group (order by id), '([^,]*)(, \1)+', 'T') as matching_empty_string
, regexp_replace(listagg(id, ', ') within group (order by id), '([^,]*)(, \1)+($|,)', '\1\3') as why_space_not_affected
from (
select 22 group_key, 1 id from dual
union all
select 22 group_key, 2 id from dual
union all
select 22 group_key, 3 id from dual
union all
select 22 group_key, 3 id from dual
)
group by group_key;
我希望为 why_space_affected_here 列保留空间(下面给出的查询): regexp_replace(listagg(id, ', ') 组内 (按 id 排序), '([^,]*)(, \1)+', '\1') as why_space_affected_here
【问题讨论】:
-
添加示例数据。添加预期输出。尝试缩短您的查询。