SMORF
SMORF

Reputation: 499

Oracle SQL - using the coalesce function

I really can't get my head around the coalesce function ... or if this is even the best way to get the result I'm trying to achieve.

I have three dates in the following script (iv.dated, iv1.dated, dh.actshpdate). When I run the following script the dates are in separate columns (as expected);

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 What I would like to achieve is one column with all dates in date order. I tried replacing my dates with ...

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

... but, I lose some of the dates;

enter image description here How can I achieve the following result?

enter image description here

Upvotes: 3

Views: 1082

Answers (1)

Lalit Kumar B
Lalit Kumar B

Reputation: 49062

You could use UNION in the following way -

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

where a, b, c, d, e, f, g are the column alias of the select list in your original query. You can give your own column alias in the UNION query.

Upvotes: 3

Related Questions