-1

您好,我需要选择一个仅包含不同值的 listagg 列。不幸的是,我使用的是 oracle 18.c,它不支持直接不同的选项,所以我可能必须使用嵌套选择(不确定?)我有以下 SQL 查询,我需要列出“adrml.email”列不同的价值观。

select distinct
c.trader_transact
, t.trader_descr
, d.third
, d.f_name
, d.def_phone
, d.def_mail
, d.f_city
, d.country_descr
, d.f_street
, con.first_name
, con.last_name
, adrph.formated_phone_nr
, link.adr
, link.contact
,adrdet.dt
,adrdet.street Contact_Street
,adrdet.post_code
,adrdet.city
,adrdet.country
,adrml.email
 ,LISTAGG(
        adrml.email,
        ' / '
    ) WITHIN GROUP(

    ORDER BY
        d.third
    )

from thr_v_third d
join tra_contract c on d.third = c.customer or d.third = c.supplier
join tra_trader t on t.trader = c.trader_transact
join thr_v_adr_lnk_contact link on link.third = d.third --and link.type = 1 and link.default_contact = 1
join adr_contact con on con.adr = link.adr and con.contact = link.contact
join adr_address_det adrdet on adrdet.adr = link.adr and adrdet.last = 1
left join adr_mail adrml on link.adr = adrml.adr and con.contact = adrml.contact and adrml.deflt = 1
left join adr_v_phones adrph on adrph.adr = link.adr and adrph.contact = link.contact and adrph.deflt = 1 and adrph.type = 1
where t.trader = 32
group by
c.trader_transact
, t.trader_descr 
 ,d.third
, d.f_name
, d.def_phone
, d.def_mail
, d.f_city
, d.country_descr
, d.f_street
, link.adr
, link.contact
, con.first_name
, con.last_name
,adrdet.dt
,adrdet.street
,adrdet.post_code
,adrdet.city
,adrdet.country
,adrml.email
, adrph.formated_phone_nr 
order by d.third

到目前为止,我在 listagg 列中收到重复的电子邮件。我怎样才能清除这个?

4

2 回答 2

0

这就是你所拥有的:

SQL> select d.dname,
  2         listagg(e.job, ', ') within group (order by e.job) jobs
  3  from dept d join emp e on e.deptno = d.deptno
  4  group by d.dname;

DNAME          JOBS
-------------- ------------------------------------------------------------
ACCOUNTING     CLERK, MANAGER, PRESIDENT
RESEARCH       ANALYST, ANALYST, CLERK, CLERK, MANAGER
SALES          CLERK, MANAGER, SALESMAN, SALESMAN, SALESMAN, SALESMAN

这就是你想要的:

SQL> select x.dname,
  2         listagg(x.job, ', ') within group (order by x.job) jobs
  3  from (select distinct d.dname,
  4                        e.job
  5        from dept d join emp e on e.deptno = d.deptno
  6       ) x
  7  group by x.dname;

DNAME          JOBS
-------------- ------------------------------------------------------------
ACCOUNTING     CLERK, MANAGER, PRESIDENT
RESEARCH       ANALYST, CLERK, MANAGER
SALES          CLERK, MANAGER, SALESMAN

SQL>

所以,是的 - 首先找到不同的值,然后是listagg它们。

于 2019-12-18T08:54:39.617 回答
0

您可以使用REGEXP_REPLACEandXMLAGG来删除重复项:

SQL> with table1(req) as
  2  (SELECT 'TEJASH' FROM DUAL UNION ALL
  3  SELECT 'RIDDHI' FROM DUAL UNION ALL
  4  SELECT 'TEJASH' FROM DUAL UNION ALL
  5  SELECT 'REKHA' FROM DUAL)
  6  SELECT
  7      REGEXP_REPLACE(RTRIM(XMLAGG(XMLELEMENT(E, REQ, ',').EXTRACT('//text()')
  8          ORDER BY
  9              REQ
 10      ).GETCLOBVAL(), ','), '([^,]+)(,\1)+', '\1') AS LIST
 11  FROM
 12      TABLE1;

LIST
--------------------------------------------------------------------------------
REKHA,RIDDHI,TEJASH

SQL>

XMLAGG使用而不是使用的原因LISTAGG是为了避免任何 4000 个字符的限制问题。

LISTAGG也可以使用以下方法来实现它REGEXP_REPLACE

SQL> with table1(req) as
  2  (SELECT 'TEJASH' FROM DUAL UNION ALL
  3  SELECT 'RIDDHI' FROM DUAL UNION ALL
  4  SELECT 'TEJASH' FROM DUAL UNION ALL
  5  SELECT 'REKHA' FROM DUAL)
  6  SELECT
  7      REGEXP_REPLACE(
  8          LISTAGG(REQ, ',') WITHIN GROUP(
  9              ORDER BY
 10                  REQ
 11          ), '([^,]+)(,\1)+', '\1') AS LIST
 12  FROM
 13      TABLE1;

LIST
--------------------------------------------------------------------------------
REKHA,RIDDHI,TEJASH

SQL>

干杯!!

于 2019-12-18T09:06:33.913 回答