I have two tables in Oracle, INVOICES and HOLDS, which I'm connecting using the invoice_id. Not all invoices will have holds, but some invoices will have multiple hold events. I want to select just one invoice and decode the release_status field. If there are multiple hold events, I need to analyse whether it's been released or not. If one line has no release code then the whole invoice is still held. Example table:
Invoice_num |Hold_Reason |Release_Lookup_code
10001 |Inv Hold |Quick release
10002 |Inv Hold |Quick release
10003 |Inv Hold |(NULL)
10004 |Inv Hold |Quick release
10004 |Inv Hold |Quick release
10004 |Amt Hold |(NULL)
10005 |Inv Hold |Variance Corrected
10005 |Inv Hold |Quick release
10006 | (NULL) |(NULL)
The result I want to show:
Invoice num |Hold Reason |Hold Status
10001 |Inv Hold |Released
10002 |Inv Hold |Released
10003 |Inv Hold |Held
10004 |Inv Hold |Held
10005 |Inv Hold |Released
10006 |(NULL) |Not Held
Currently I only have this code, which brings back multiple lines:
select
inv.invoice_num,
hold.hold_reason,
(CASE WHEN hold.invoice_id is not null
then (CASE WHEN HOLD.RELEASE_LOOKUP_CODE is null then 'HELD' else 'RELEASED' END)
else 'NOT HELD' END) Hold_Status
from AP.AP_INVOICES_ALL INV,
AP.AP_HOLDS_ALL HOLD
WHERE inv.invoice_id = hold.invoice_id (+)
order by 1
I thought of counting the number of invoices, but after that I still need to analyse the content of the Release_Lookup_Code field. I'm not looking for full answers, just some suggestions as to how I should proceed.
10004showInv Holdinstead ofAmt Hold?invoice_idorinvoice_num, as your query uses both?)