I was trying to concatenate distinct values of rows using LISTAGG and REGEXP_REPLACE functions as described in this link: LISTAGG in Oracle to return distinct values.
I am trying to understand what is going on, I made some changes and started playing with it. However, I wasn't getting the answer I was expecting. It seems like I'm missing some key concept on how "empty string" and "space" are treated by the regexp engine. How does the regexp engine process the why_space_affected_here, why_space_affected_here, why_space_not_affected_here, matching_empty_string, and why_space_not_affected columns in the below sample query?
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;
I was expecting the spaces to be reserved for the why_space_affected_here column (the query given below):
regexp_replace(listagg(id, ', ') within group (order by id), '([^,]*)(, \1)+', '\1') as why_space_affected_here
.