我想消除重复并显示一次
例如
SELECT 'apple, apple, orange'
FROM dual;
我想展示
apple, orange
另一个例子。
SELECT 'apple, apple, apple, apple,'
FROM dual;
我只想展示
apple
此代码显示
with data as
(
select 'apple, apple, apple, apple' col from dual
)
select listagg(col, ',') within group(order by 1) col
from (
select distinct regexp_substr(col, '[^,]+', 1, level) col
from data
connect by level <= regexp_count(col, ',')
)
答案 0 :(得分:2)
这样的事情将消除重复:
with temp as
(
select 1 Name, 'test1' Project, 'apple, apple, orange' Error from dual
union all
select 2, 'test2', 'apple, apple, apple, apple,' from dual
), split as (
select distinct
t.name, t.project,
trim(regexp_substr(t.error, '[^,]+', 1, levels.column_value)) as error
from
temp t,
table(cast(multiset(select level
from dual connect by level <= length (regexp_replace(t.error, '[^,]+')) + 1) as sys.OdciNumberList)) levels
)
SELECT Name, listagg(Error, ',') within group(order by 1) as result
FROM split
GROUP BY Name
输出
如您所见,您会得到一个NULL,因为多余的逗号,
答案 1 :(得分:1)
Oracle forum中有以下几种选择:
with data as ( select '5,5,5,5,6,6,5,5,5,6,7,4,1,2,1,4,7,2' col from dual ) select listagg(col, ',') within group(order by 1) col from ( select distinct regexp_substr(col, '[^,]+', 1, level) col from data connect by level <= regexp_count(col, ',') )
只需将数字替换为'apple, apple, orange'
答案 2 :(得分:1)
将trim
和distinct
与regexp
函数一起使用对于获得所需的结果非常重要
select listagg(str,',') within group (order by 0) as Result
from
(
select distinct trim(regexp_substr('apple, apple, orange','[^,]+', 1, level)) as str
from dual
connect by level <= regexp_count('apple, apple, orange',',') + 1
);