简体   繁体   中英

Oracle SQL - remove duplicates from listagg

The following script gives me a one line table, showing 'manuf_skus' and 'distr_skus' in one row (using the 'listagg' function)...

select 
pd.part_no, 
pd.catnr, 
pd.prodtyp, 
pd.packtyp, 
pd.description, 
ip.purchase_type,
li.cust_catnr categroy,
li.licence_expire,
listagg (nrb.p_catnr, ', ') WITHIN GROUP (ORDER BY nrb.p_catnr) manuf_skus,
listagg (st.selection_no, ', ') WITHIN GROUP (ORDER BY st.selection_no) distr_skus,
nvl(pd.fod_idc, 'N') FOD_IDC, 
(select max(acp.qty_free_idc)    
    from oes_fod_match acp 
    where pd.part_no = acp.part_no 
    and acp.type = 'SUP' 
    and acp.cre_dat = (select max(acp1.cre_dat) 
                        from oes_fod_match acp1 
                        where acp.part_no = acp1.part_no 
                        and acp1.qty_free > 0 
                        and acp1.type = 'SUP')) qty_free_idc, 
(select nvl(sum(pl.qty_onhand), 0) 
    from part_loc pl 
    where pl.part_no = pd.part_no 
    and pl.location_type = 'IN') total_stock_comp,
(select nvl(max(pp.qty), 0) 
    from oes_purpos pp 
    where pp.av_part_no = pd.part_no 
    and pp.c_status != 'D' 
    and pp.datneu = (select max(pp1.datneu) 
                        from oes_purpos pp1 
                        where pp1.av_part_no = pp.av_part_no 
                        and pp1.c_status != 'D')) last_aw_po_qty,
(select nvl(sum(ps.requ_qty - nvl(ps.del_qty, 0)), 0) 
    from oes_purpos pp, 
    oes_purseg ps 
    where ps.headnr = pp.headnr 
    and ps.posnr = pp.posnr 
    and pp.av_part_no = pd.part_no 
    and ps.c_status not in ('9', 'D') 
    and ps.o_status not in ('D', '9')) open_aw_po_qty,
(select sum(al.qty_required) 
    from allocations al 
    where pd.part_no = al.component_part 
    and al.status_code between '4' and '8') comp_alloc
FROM part_description pd 
INNER JOIN inventory_purchase ip 
   ON ip.part_no = pd.part_no 
INNER JOIN scm_prodtyp pt 
   ON pt.prodtyp = pd.prodtyp 
INNER JOIN oes_fod_match acp 
   ON acp.part_no = pd.part_no
INNER JOIN leos_item li
   ON pd.part_no = li.av_part_no
INNER JOIN inventory_purchase ip 
   ON ip.part_no = pd.part_no 
INNER JOIN scm_prodtyp pt 
   ON pt.prodtyp = pd.prodtyp 
INNER JOIN oes_fod_match acp 
   ON acp.part_no = pd.part_no 
INNER JOIN part_description pd3 
   ON pd3.part_no = pd.part_no 
INNER JOIN NCF_COMPPART ncf 
   ON ncf.item_part_no = pd.part_no 
INNER JOIN oes_nrbom nrb 
   ON ncf.catnr = nrb.c_catnr 
  AND ncf.prodtyp = nrb.c_prodtyp 
  AND ncf.packtyp = nrb.c_packtyp 
  AND ncf.vernr = nrb.c_vernr
INNER JOIN part_description pd2
   ON nrb.p_catnr = pd2.catnr
  AND nrb.p_prodtyp = pd2.prodtyp
  AND nrb.p_packtyp = pd2.packtyp
LEFT OUTER JOIN BDS_SELECTION_ORG@sid_to_cdsuk st
   ON 9||nrb.p_catnr = st.selection_no
WHERE pd.cunr in ('649830', 'W30000') 
   and pd.catnr = '2EDVD0017'
   and pd.fod_idc = 'Y' 
   and pt.prodgrp = 'AW'
GROUP BY
   pd.part_no, 
   pd.catnr, 
   pd.prodtyp, 
   pd.packtyp, 
   pd.description,
   ip.purchase_type,
   li.cust_catnr,
   li.licence_expire, 
   pd.fod_idc

What I would like to add is the following subquery ...

(select sum(ds.planqty - nvl(ds.delqty, 0)) 
    from oes_opos op, 
    oes_oposdelseg ds
    where ds.ordnr = op.ordnr 
    and ds.posnr = op.posnr
    and op.catnr = nrb.p_catnr 
    and op.prodtyp = nrb.p_prodtyp 
    and op.packtyp = nrb.p_packtyp
    and ds.c_status not in ('9', 'D') 
    and op.ol_typ = 'XX'
    group by
    pd.catnr, nrb.catnr, nrb.prodtyp, nrb.packtyp) open_manuf_qty

... to make this work I have to add the following GROUP BY's

nrb.p_catnr,
nrb.p_prodtyp,
nrb.p_packtyp

But, when I add this subquery, the 'manuf_skus' and 'distr_skus' are no longer in one row. I assume this is because I've had to add the extra group by's. Is there of adding this sum subquery and still keeping one row?

Thanks All

Are the additional rows duplicates? If so, you might try adding a distinct:

select distinct 
pd.part_no, 
pd.catnr, 
pd.prodtyp, 

....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM