【问题标题】:Oracle - unique Listagg valuesOracle - 唯一的 Listagg 值
【发布时间】:2016-02-09 17:51:14
【问题描述】:

我是使用 Listagg 的新手。以下脚本适用于它为我提供了一个值列表,但是该列表重复了这些值。

是否可以使用 Listagg 只返回一个唯一的值列表。

我正在使用 oracle 10g。

select distinct ds.catnr,ds.planqty, ds.ordnr, ds.posnr, ds.segnr, 
listagg(case when not li.paco is null then (select unique max(li1.paco) from leos_item li1 where li.av_part_no = li1.av_part_no) end, ', ') within group (order by pd.part_no) inq_no
from oes_delsegview ds, oes_address ad, oes_opos op, oes_nrbom nr, scm_prodtyp sp, leos_item li, part_description pd
where ds.delnr = ad.key
and ad.adr = ds.deladr
and ds.pos_o_status not in ('9', 'D')
and ds.pos_c_status not in ('9', 'D')
and ds.seg_o_status not in ('9', 'D')
and ds.seg_c_status not in ('9', 'D')
and ds.cunr in ('W31170','W31172')
and ds.pos_type != 'RC'
and ds.ordnr = op.ordnr
and ds.posnr = op.posnr
and ds.catnr = pd.catnr
and ds.prodtyp = pd.prodtyp
and ds.packtyp = pd.packtyp
and ds.catnr = nr.p_catnr (+)
and ds.prodtyp = nr.p_prodtyp (+)
and ds.packtyp = nr.p_packtyp (+)
and nr.c_prodtyp = sp.prodtyp (+) 
and sp.prodgrp (+) = 'COMP'
and substr(nr.c_prodtyp,1,2) not in ('MT','LF')
and nr.c_catnr = li.catnr (+)
and nr.c_prodtyp = li.prodtyp (+)
and nr.c_packtyp = li.packtyp (+)
and pd.catnr = '9780007938797'
group by ds.catnr,ds.planqty, ds.ordnr, ds.posnr, ds.segnr

我的 Listagg 的结果是:

14/061127-12, 14/061127-16, 14/061127-16, 14/061127-16, 14/061127-16, 14/061127-16, 14/061127-16, 14/061127-16, 14/061127-16, 14/061127-16, 14/061127-16, 14/061127-16, 14/061127-16, 14/061127-16, 14/061127-16, 14/061127-16, 14/061127-16, 14/061127-16, 14/061127-16, 14/061127-16, 14/061127-16

我想看到的是:

14/061127-12, 14/061127-16

任何帮助将不胜感激。

【问题讨论】:

标签: sql oracle oracle10g listagg


【解决方案1】:

我删除了第一个distinct,因为您已经在Select 查询中的所有字段都使用了group by,并将case when 替换为select 查询:

select ds.catnr,ds.planqty, ds.ordnr, ds.posnr, ds.segnr, 
    listagg((select distinct max(li1.paco) from leos_item li1 where li.av_part_no = li1.av_part_no and li.paco is not null), ', ') within group (order by pd.part_no) inq_no
    from oes_delsegview ds, oes_address ad, oes_opos op, oes_nrbom nr, scm_prodtyp sp, leos_item li, part_description pd
    where ds.delnr = ad.key
    and ad.adr = ds.deladr
    and ds.pos_o_status not in ('9', 'D')
    and ds.pos_c_status not in ('9', 'D')
    and ds.seg_o_status not in ('9', 'D')
    and ds.seg_c_status not in ('9', 'D')
    and ds.cunr in ('W31170','W31172')
    and ds.pos_type != 'RC'
    and ds.ordnr = op.ordnr
    and ds.posnr = op.posnr
    and ds.catnr = pd.catnr
    and ds.prodtyp = pd.prodtyp
    and ds.packtyp = pd.packtyp
    and ds.catnr = nr.p_catnr (+)
    and ds.prodtyp = nr.p_prodtyp (+)
    and ds.packtyp = nr.p_packtyp (+)
    and nr.c_prodtyp = sp.prodtyp (+) 
    and sp.prodgrp (+) = 'COMP'
    and substr(nr.c_prodtyp,1,2) not in ('MT','LF')
    and nr.c_catnr = li.catnr (+)
    and nr.c_prodtyp = li.prodtyp (+)
    and nr.c_packtyp = li.packtyp (+)
    and pd.catnr = '9780007938797'
    group by ds.catnr,ds.planqty, ds.ordnr, ds.posnr, ds.segnr

【讨论】:

  • 嗨 haytem,谢谢,但我仍然有相同的结果(重复值列表)。
【解决方案2】:

我使用了一个 regexp_replace 函数来删除我的 listagg 中的重复项;

regexp_replace(
    listagg((select distinct max(li1.paco) from leos_item li1 where li.av_part_no = li1.av_part_no and li.paco is not null), ', ') within group (order by pd.part_no) 
    ,'([^,]+)(,\1)+', '\1') inq_no

看起来工作正常。

LISTAGG in oracle to return distinct values

【讨论】:

  • 这个正则表达式替换有一个错误。尝试reviews,search: 替换为“reviewsearch”。我会将已修复的问题作为单独的答案发布。
【解决方案3】:

您可以通过 RegEx 替换来做到这一点。这是一个例子:

-- Citations Per Year - Cited Publications main query. Includes list of unique associated core project numbers, ordered by core project number.
SELECT ptc.pmid AS pmid, ptc.pmc_id, ptc.pub_title AS pubtitle, ptc.author_list AS authorlist,
  ptc.pub_date AS pubdate,
  REGEXP_REPLACE( LISTAGG ( ppcc.admin_phs_org_code || 
    TO_CHAR(ppcc.serial_num,'FM000000'), ',') WITHIN GROUP (ORDER BY ppcc.admin_phs_org_code || 
    TO_CHAR(ppcc.serial_num,'FM000000')),
    '(^|,)(.+)(,\2)+', '\1\2')
  AS projectNum
FROM publication_total_citations ptc
  JOIN proj_paper_citation_counts ppcc
    ON ptc.pmid = ppcc.pmid
   AND ppcc.citation_year = 2013
  JOIN user_appls ua
    ON ppcc.admin_phs_org_code = ua.admin_phs_org_code
   AND ppcc.serial_num = ua.serial_num
   AND ua.login_id = 'EVANSF'
GROUP BY ptc.pmid, ptc.pmc_id, ptc.pub_title, ptc.author_list, ptc.pub_date
ORDER BY pmid;

【讨论】:

    【解决方案4】:

    超级简单的答案解决了

    select
    regexp_replace('14/061127-12, 14/061127-16, 14/061127-16','([^,]+)(,\1)*(,|$)', '\1\3')
    
    
    from dual
    

    ->

    14/061127-12、14/061127-16

    查看我的完整答案here

    【讨论】:

    • 除非首先对字符串进行排序,否则不起作用。或者更确切地说,所有重复项必须在列表中彼此相邻才能使此正则表达式起作用。
    • 在上面的链接中查看我的完整答案
    猜你喜欢
    • 2021-06-03
    • 1970-01-01
    • 1970-01-01
    • 2012-07-15
    • 2021-03-09
    • 2021-07-25
    • 2020-12-31
    • 1970-01-01
    • 2011-11-13
    相关资源
    最近更新 更多