0

I am using regex to string unique sources. My issue is the sources are merging:

i.e. sources [manager, ream, ream, ream]

Needed output [manager, ream]

Output receiving [manageream, ream]

SELECT regexp_replace( listagg(c.source, ',')
within group(order by c.person_no) , 
'([^,]+)(,\1)+', '\1') source 
FROM table c

How do I fix my above code to get the needed output?

1
  • The real problem is listagg. You can do this more easily without it. Commented May 16, 2016 at 13:25

1 Answer 1

1

If I understand your need, you may simply need DISTINCT:

SQL> create table test(source) as (
  2  select 'manager' from dual union all
  3  select 'ream'    from dual union all
  4  select 'ream'    from dual union all
  5  select 'ream'    from dual
  6  );

Table created.

SQL> select listagg(source, ',') within group (order by 1)
  2  from (
  3          select distinct source
  4          from test
  5       );

LISTAGG(SOURCE,',')WITHINGROUP(ORDERBY1)
----------------------------------------------------------------------------
manager,ream

SQL>

The inner query is used to only get the distinct values, while the outer makes the concatenation; you can rewrite this in different ways, I believe this is one of the most readable.

Sign up to request clarification or add additional context in comments.

2 Comments

hi thank you for the solution you provided, however I am still getting (manager, ream, ream) having "ream" duplicated in the result
what if you execute select distinct source from table? maybe you have similar but not equal values, for example due to blank chars?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.