一尘不染

如何消除列中的重复数据

sql

我想消除重复并显示一次

例如

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, ',')
       )

阅读 132

收藏
2021-05-30

共1个答案

一尘不染

这样的事情将消除重复:

SQL演示

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
2021-05-30