Hi,
I am trying to get the invoice data from the invoice_head table(single row) with the invoice_lines(purchase orders) on certain conditions.
The where is fine but SQL doesn't like binding any of the invoice_lines columns.Which why I have tried putting in a nested select statement.
SELECT TOP 1 h.Invoice_id AS Invoice_Id, h.IssueDate AS [Invoice Date], h.Optional_1 AS [Division Buyer],
h.ScanDate AS [Scan Date], h.Supplier AS [Supplier/Vendor], h.Optional_2 AS [PO Number],
h.InvoiceNumber AS [Invoice Number], h.Rejected, h.Completed AS [Transaction Date], h.Notified,
h.Comments, h.Transferred, h.Optional_3 AS [Invoice Type], h.InvoiceTotal AS [Total Invoice Amount],
h.Optional_4 AS TFN, h.Optional_5 AS Orginator, h.Amount_1 AS [GST Total Amount], h.Responsible,
h.IsCredit AS [Credit Note], h.Currency, h.PaymentDate AS [BCC Upload Date1],
h.Optional_7 AS [Supplier Code]
FROM
Invoice_head h
INNER JOIN
(SELECT
l.Invoice_id, l.Account AS [Account Code], l.Optional_6 AS [Job Number],
l.Optional_8 AS [Cost Type Code], l.Department AS [Department Code], l.Optional_7 AS [Employee Code],
l.Optional_1 AS [Product Code], l.Optional_9 AS [Campaign Code], l.Optional_10 AS Optional,
l.LineText AS Description, l.Approver AS [Completed By], l.Optional_3 AS [Division Code],
l.ApproveDate AS [BCC Upload Date], l.Optional_5 AS [Client Code], l.Responsible AS [Previous Approvers],
l.Remarks, l.Amount_2 AS [Total Excl GST], l.Amount_3 AS GST, l.LineTotal AS [Total Inc GST]
FROM
Invoice_lines
GROUP BY
Invoice_id) l
ON h.invoice_id = l.invoice_id
WHERE (companies.company_id = 44)AND (Invoice_lines..Invoice_id ='135291') AND (invoice_lines.ApproveDate >= (CASE WHEN datepart(dw, getdate()) = 2 THEN (getdate() - 3) ELSE (getdate() - 1)
END))
GROUP BY h.invoice_id
ERROR: Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "l.Invoice_id" could not be bound. etc...
I have attached a mediafire file showing exactly what I trying to get SQL to do.
I am trying to get the invoice data from the invoice_head table(single row) with the invoice_lines(purchase orders) on certain conditions.
The where is fine but SQL doesn't like binding any of the invoice_lines columns.Which why I have tried putting in a nested select statement.
SELECT TOP 1 h.Invoice_id AS Invoice_Id, h.IssueDate AS [Invoice Date], h.Optional_1 AS [Division Buyer],
h.ScanDate AS [Scan Date], h.Supplier AS [Supplier/Vendor], h.Optional_2 AS [PO Number],
h.InvoiceNumber AS [Invoice Number], h.Rejected, h.Completed AS [Transaction Date], h.Notified,
h.Comments, h.Transferred, h.Optional_3 AS [Invoice Type], h.InvoiceTotal AS [Total Invoice Amount],
h.Optional_4 AS TFN, h.Optional_5 AS Orginator, h.Amount_1 AS [GST Total Amount], h.Responsible,
h.IsCredit AS [Credit Note], h.Currency, h.PaymentDate AS [BCC Upload Date1],
h.Optional_7 AS [Supplier Code]
FROM
Invoice_head h
INNER JOIN
(SELECT
l.Invoice_id, l.Account AS [Account Code], l.Optional_6 AS [Job Number],
l.Optional_8 AS [Cost Type Code], l.Department AS [Department Code], l.Optional_7 AS [Employee Code],
l.Optional_1 AS [Product Code], l.Optional_9 AS [Campaign Code], l.Optional_10 AS Optional,
l.LineText AS Description, l.Approver AS [Completed By], l.Optional_3 AS [Division Code],
l.ApproveDate AS [BCC Upload Date], l.Optional_5 AS [Client Code], l.Responsible AS [Previous Approvers],
l.Remarks, l.Amount_2 AS [Total Excl GST], l.Amount_3 AS GST, l.LineTotal AS [Total Inc GST]
FROM
Invoice_lines
GROUP BY
Invoice_id) l
ON h.invoice_id = l.invoice_id
WHERE (companies.company_id = 44)AND (Invoice_lines..Invoice_id ='135291') AND (invoice_lines.ApproveDate >= (CASE WHEN datepart(dw, getdate()) = 2 THEN (getdate() - 3) ELSE (getdate() - 1)
END))
GROUP BY h.invoice_id
ERROR: Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "l.Invoice_id" could not be bound. etc...
I have attached a mediafire file showing exactly what I trying to get SQL to do.