我需要一个oracle查询来删除值中的重复项

时间:2018-04-18 08:30:25

标签: sql csv oracle11g split

 FILM_ID    TITLE       
---------- ---------- 
    1       Frozen,Frozen         
    2       Frozen,High,Low,High 


 FILM_ID    TITLE       
---------- ---------- 
    1       Frozen
    2       Frozen,High,Low

如果值为Frozen,Frozen我需要查询才能获得冻结以及第二个值我应该Frozen,High,Low

2 个答案:

答案 0 :(得分:0)

试试这个解决方案:

SELECT film_id, LISTAGG(title,',') WITHIN GROUP (Order BY title) TITLE
FROM 
(select distinct film_id, 
        regexp_substr(Title,'[^,]+', 1, level) as title
 from Table1
 connect by regexp_substr(Title, '[^,]+', 1, level) is not null
) t
GROUP BY film_id

<强>输出:

FILM_ID |  TITLE
--------------------------
 1      |  Frozen
 2      |  Frozen,High,Low

链接演示:

  

http://sqlfiddle.com/#!4/12377c/14

答案 1 :(得分:0)

这是一个选项:

  • TEST是你的表
  • INTER以逗号分隔值创建行; DISTINCT在这里只返回不同的值(就像你想要的那样)
  • 最终查询将它们聚合回来
SQL> with test (film_id, title) as
  2    (select 1, 'Frozen,Frozen'        from dual union
  3     select 2, 'Frozen,High,Low,High' from dual
  4    ),
  5  inter as
  6    (select distinct film_id, regexp_substr(title, '[^,]+', 1, column_value) title
  7     from test,
  8          table(cast(multiset(select level from dual
  9                              connect by level <= regexp_count(title, ',') + 1
 10                             ) as sys.odcinumberlist))
 11    )
 12  select film_id, listagg(title, ',') within group (order by title) title
 13  from inter
 14  group by film_id;

   FILM_ID TITLE
---------- ------------------------------
         1 Frozen
         2 Frozen,High,Low