消除使用Oracle LISTAGG函数重复[复制](Eliminate duplicates us

2019-07-30 13:39发布

可能重复:
LISTAGG在Oracle返回不同的值

我使用的是Oracle LISTAGG功能,但我回到名单之内我其实想消除重复,只返回不同的值。

查询我已经是这样的:

select a.id,
       a.change_id,
       LISTAGG(b.name, ',') WITHIN GROUP (ORDER BY b.name) AS "Product Name", 
from   table_a a,
       table_b b
where  a.id = 1
and    b.change_id = c.change_id
group by a.id, a.change_id

目前,它返回(只是显示一个记录):

1    1   NameA, NameA, NameB, NameC, NameD, Name D

我想回的是:

1    1   NameA, NameB, NameC, Name D

Answer 1:

正如评论链接的答案不提供我的解决方案的味道,无论如何,我会张贴。

我只用table_b虚拟数据显示的概念,你可以轻松地添加您的加入等:

with table_b as ( -- dummy data
 select 'name'||mod(level,3) name
        ,mod(level,3) id
   from dual
  connect by level < 10
 union all
 select 'name'||mod(level,2) name
        ,mod(level,3) id
   from dual
  connect by level < 10
)
select id
      ,RTRIM (
              XMLAGG (
                      XMLELEMENT (E,XMLATTRIBUTES (name|| ',' AS "Seg")
                      )
                     ORDER BY name ASC
              ).EXTRACT ('./E[not(@Seg = preceding-sibling::E/@Seg)]/@Seg'),
              ','
             ) AS "Product Name"
       ,LISTAGG(b.name, ',') WITHIN GROUP (ORDER BY b.name) AS "Product Name with dups"
  from table_b b
group by id;

(取自思想https://forums.oracle.com/forums/thread.jspa?messageID=9634767&tstart=0#9943367 )



文章来源: Eliminate duplicates using Oracle LISTAGG function [duplicate]