| Regular Expression [message #644940] |
Fri, 20 November 2015 07:47  |
 |
samyms
Messages: 18 Registered: May 2015 Location: Chennai
|
Junior Member |
|
|
I want to remove duplicate record from Listagg function.
Combination of duplicate Value comes as
1) PAY,PAY,PAY,PAY
2) ACP,PAY,PAY
3) PAY,ACP,ACP
4) ACP,ACP
create table paym (method varchar2(100));
insert into paym values('PAY,PAY,PAY,PAY');
insert into paym values('ACP,PAY,PAY');
insert into paym values('PAY,ACP,ACP');
insert into paym values('PAY,PAY');
SELECT method ,
COUNT(*) AS method_cnt ,
LISTAGG(method, ',') WITHIN GROUP (ORDER BY method) AS List_Agg,
REGEXP_REPLACE(LISTAGG(method, ',') WITHIN GROUP (ORDER BY method),'([^,]+)(,\1)+', '\1') AS List_Agg_Distinct
FROM paym
GROUP BY method
Its not removing the duplicate correctly. Can you please help me.
Output should be :
1. PAY
2. ACP,PAY
3. PAY,ACP
4. ACP
|
|
|
|
| Re: Regular Expression [message #644942 is a reply to message #644940] |
Fri, 20 November 2015 07:52   |
cookiemonster
Messages: 13975 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
Listagg groups data from different records into a single delimited string.
Your data is already a delimited string, so why are you trying to use listagg?
|
|
|
|
|
|
|
|
|
|
| Re: Regular Expression [message #644946 is a reply to message #644940] |
Fri, 20 November 2015 08:25   |
 |
Michel Cadot
Messages: 68776 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
SQL> with
2 data as (select method, rownum id from paym),
3 words as (
4 select distinct id, regexp_substr(method, '[^,]+', 1, column_value) word
5 from data,
6 table(cast(multiset(select level from dual
7 connect by level <= regexp_count(method,',')+1)
8 as sys.odciNumberList))
9 )
10 select listagg(word,',') within group (order by word) method
11 from words
12 group by id
13 order by id
14 /
METHOD
--------------------
PAY
ACP,PAY
ACP,PAY
PAY
4 rows selected.
|
|
|
|
| Re: Regular Expression [message #644947 is a reply to message #644943] |
Fri, 20 November 2015 08:43   |
cookiemonster
Messages: 13975 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
samyms wrote on Fri, 20 November 2015 13:57
Note: The delimited string are made by Listagg but for testing pupose i just created like that.
So the actual data in the table isn't stored in that form?
Then you should post the data as it's actually stored, otherwise any solution provided won't work on what you've really got.
|
|
|
|
| Re: Regular Expression [message #644948 is a reply to message #644940] |
Fri, 20 November 2015 09:02   |
Solomon Yakobson
Messages: 3312 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
select xmlquery(('string-join(distinct-values(ora:tokenize("' || method || '",",")),",")' ) returning content) method
from paym
/
METHOD
---------
PAY
ACP,PAY
PAY,ACP
PAY
SQL>
SY.
|
|
|
|
|
|