The following SQL code creates a two-line table with an invoice on one line and payment grouped on the other line. However that’s not the desired representation. The real goal is to display it as statement, where the invoice is at the top, followed by a list of payments ordered by date.
Is it possible to write a query to accomplish that, based on the information shown? (Feel free to request more information). Can anyone suggest an approach?
Here is the SQL SELECT code:
SELECT FilteredInvoice.accountidname,
FilteredInvoice.createdon,
FilteredInvoice.duedate,
FilteredInvoice.invoicenumber,
FilteredInvoice.statecodename,
FilteredInvoice.totalamount_base,
FilteredMag_Payment.mag_paymentdate,
FilteredMag_Payment.mag_amount_base,
GETDATE() AS Today
FROM FilteredInvoice
LEFT OUTER JOIN FilteredAccount ON FilteredInvoice.accountid = FilteredAccount.accountid
LEFT OUTER JOIN FilteredMag_Payment ON FilteredInvoice.invoiceid = FilteredMag_Payment.mag_invoiceid
WHERE (FilteredInvoice.statecodename <> N'Canceled')
ORDER BY FilteredInvoice.createdon
There are some oddities about the original query – is the Account ID Name really held on the Invoice table, rather than the Account table? The left outer join from Invoice to Account also makes it look as though there could be Invoices without corresponding Accounts – it would be more normal to assume the converse, especially in a statement report.
Assuming the original query is selecting the required data correctly, I suggest: