Oracle SQL - 使用coalesce函数

时间:2015-02-03 10:30:57

标签: sql oracle coalesce

我真的无法理解合并功能......或者如果这是获得我想要实现的结果的最佳方式。

我在以下脚本中有三个日期(iv.dated,iv1.dated,dh.actshpdate)。当我运行以下脚本时,日期在不同的列中(如预期的那样);

select unique li.catnr, li.av_part_no, li.artist||' / '||li.title description, li.cust_catnr pallet_ref,
trunc(iv.dated), trunc(iv1.dated), trunc(dh.actshpdate)
from leos_item li
left join invtran_view_oes iv
    on li.av_part_no = iv.part_no 
    and (iv.transaction = 'NREC' and iv.location_no = '        RETURNS    W')
left join invtran_view_oes iv1
    on li.av_part_no = iv1.part_no
    and (iv1.transaction = 'CORR+' and iv1.remark like 'STOCK FROM SP PALLET%')
left join oes_delsegview od
    on od.catnr = li.catnr
    and od.prodtyp = li.prodtyp
    and od.packtyp = li.packtyp
left join oes_dpos dp
    on od.ordnr = dp.ordnr
    and od.posnr = dp.posnr
    and od.segnr = dp.segnr
left join oes_dhead dh
    on dp.dheadnr = dh.dheadnr
where li.cunr = '816900'
and substr(li.catnr,1,5) in ('RGMCD','RGJCD')
and li.item_type = 'FP'
and li.catnr = 'RGJCD221'

enter image description here 我想要实现的是一列,所有日期都按日期顺序排列。 我尝试用...替换我的约会。

trunc(coalesce(iv.dated, iv1.dated, dh.actshpdate)) transaction_date

......但是,我失去了一些日期;

enter image description here 如何实现以下结果?

enter image description here

1 个答案:

答案 0 :(得分:3)

您可以通过以下方式使用 UNION -

WITH DATA AS(
<your query goes here>
)
SELECT A, b, c, d, e FROM DATA
UNION
SELECT A,b,c,d,f FROM DATA
UNION
SELECT A,b,c,d,g FROM DATA

其中a,b,c,d,e,f,g是原始查询中选择列表的列别名。您可以在UNION查询中提供自己的列别名。