Hi all,
I have a query below which returns a list of invoices from the PL_TRANSATIONS table. This was fine but I now need a field from the detail line tavle SL_PL_NL_DETAIL. I've added this in but I onviously now get mutiple rows where the invoice is multi lined. I need to amend the query so it only pulls back the 1st detail line per invoice. The analysis data I need is the same on each detail line.
PT_HEADER_REF is the unique invoice
The lowest DET_HEADER_KEY for an invoice line will be the first.
So I really want the first line by selecting the lowest header key per det_header_key,
How can I amend the query below to do this?
SELECT
PT_PRIMARY 'InvoiceID',
PT_HEADER_REF,
PT_NETT,
PT_VAT,
PT_GROSS,
isnull(SU_USRCHAR1,'') Approver1,
isnull(SU_USRCHAR2,'') Approver2,
isnull(SU_USRCHAR5,'') Approver3,
substring(DET_ANALYSIS,4,3) Department
FROM PL_TRANSACTIONS
left join PL_ACCOUNTS2 on SUCODE2 = PT_COPYSUPP
left join SL_PL_NL_DETAIL on DET_HEADER_KEY = PT_HEADER_KEY
I have a query below which returns a list of invoices from the PL_TRANSATIONS table. This was fine but I now need a field from the detail line tavle SL_PL_NL_DETAIL. I've added this in but I onviously now get mutiple rows where the invoice is multi lined. I need to amend the query so it only pulls back the 1st detail line per invoice. The analysis data I need is the same on each detail line.
PT_HEADER_REF is the unique invoice
The lowest DET_HEADER_KEY for an invoice line will be the first.
So I really want the first line by selecting the lowest header key per det_header_key,
How can I amend the query below to do this?
SELECT
PT_PRIMARY 'InvoiceID',
PT_HEADER_REF,
PT_NETT,
PT_VAT,
PT_GROSS,
isnull(SU_USRCHAR1,'') Approver1,
isnull(SU_USRCHAR2,'') Approver2,
isnull(SU_USRCHAR5,'') Approver3,
substring(DET_ANALYSIS,4,3) Department
FROM PL_TRANSACTIONS
left join PL_ACCOUNTS2 on SUCODE2 = PT_COPYSUPP
left join SL_PL_NL_DETAIL on DET_HEADER_KEY = PT_HEADER_KEY