2016-05-16 24 views
0

我正在使用正則表達式來字符串唯一的來源。我的問題是源合併:正則表達式在SQL中沒有正確分組

即源[經理,令,令,令]

所需輸出[經理,令]

輸出接收[manageream,令]

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

如何修復我的上述代碼以獲得需要的輸出

+0

真正的問題是'listagg'。沒有它你可以更容易地做到這一點。 – Dan

回答

1

如果我理解你的需求,你可能只需要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> 

內部查詢只用於獲得不同的值,而外部使得串聯;你可以用不同的方式重寫,我相信這是最可讀的。

+0

嗨,感謝您提供的解決方案,但是我仍然得到(經理,令,令)「結果」複製 – Ariel

+0

如果您執行'select distinct from table'?也許你有類似但不相等的價值,例如由於空白字符? – Aleksej