I have a SQL Query containing a
REGEXP_REPLACE(LISTAGG(foo.name, ',') WITHIN GROUP (ORDER BY foo.name), '([^,]+)(,\1)+', '\1') AS bar
in its SELECT
. LISTAGG
concatenates the values of the foo.name column with a ',' as separator, whereas REGEXP_REPLACE
replaces duplicates.
When I change the LISTAGG
so that a ", " (comma followed by a whitespace character) is used as separator, how do I have to adjust the REGEXP_REPLACE
?
Edit:
When changing the REGEXP_REPLACE
to '([^,]+)(, \1)+'
it seems to work and I get
CITRONENSÄURE, KALIUMSORBAT, PEKTIN
But when changing the REGEXP_REPLACE
to '^([^,]+)(, \1)+$'
I still have duplicates:
CITRONENSÄURE, CITRONENSÄURE, CITRONENSÄURE, CITRONENSÄURE, CITRONENSÄURE, KALIUMSORBAT, KALIUMSORBAT, KALIUMSORBAT, KALIUMSORBAT, KALIUMSORBAT, PEKTIN, PEKTIN, PEKTIN, PEKTIN, PEKTIN
So, is '([^,]+)(, \1)+'
the right one, regardless of having no ^
and $
?