Following on from the question I originally asked yesterday (here), I was able to construct the following sql query that producded a running list of invoices and payments.
SELECT
'Invoice' AS TransactionType,
i.InvoiceNumber AS Description,
i.InvoiceDate AS TransactionDate,
CAST(ROUND(i.OutstandingBalance, 2) AS DECIMAL(12, 2)) AS TransactionAmount
FROM
Invoices i
WHERE
i.CustomerId = 12
AND i.InvoiceDate BETWEEN '20150601' AND '20160229'
AND i.OutstandingBalance > 0.02
UNION
SELECT
'Payment' AS TransactionType,
ip.InvoicePaymentId AS Description,
ip.InvoicePaymentDate AS TransactionDate,
- ip.Amount AS TransactionAmount
FROM
InvoicePayments ip
WHERE
ip.CustomerId = 12
AND ip.InvoicePaymentDate BETWEEN '20150601' AND '20160229'
ORDER BY
TransactionDate
What I would now like to do is produce one extra column that is in effect the running balance on the account. I figured that if I started with a variable it should then be possible to add (or subtract from it to give me what I wanted). To that end I tried the following;
DECLARE @OutstandingBalance MONEY = 0
SELECT
'Invoice' AS TransactionType, i.InvoiceNumber AS Description,
i.InvoiceDate AS TransactionDate,
CAST(ROUND(i.OutstandingBalance, 2) AS DECIMAL(12, 2)) AS TransactionAmount,
@OutstandingBalance + CAST(ROUND(i.OutstandingBalance, 2) AS DECIMAL(12, 2)) AS Balance
FROM
Invoices i
WHERE
i.CustomerId = 12
AND i.InvoiceDate BETWEEN '20150601' AND '20160229'
AND i.OutstandingBalance > 0.02
Which produced the results below.
However trying to modify the query by making it @OutstandingBalance += like so;
DECLARE @OutstandingBalance MONEY = 0
SELECT
'Invoice' AS TransactionType, i.InvoiceNumber AS Description,
i.InvoiceDate AS TransactionDate,
CAST(ROUND(i.OutstandingBalance, 2) AS DECIMAL(12, 2)) AS TransactionAmount,
@OutstandingBalance += CAST(ROUND(i.OutstandingBalance, 2)AS DECIMAL(12,2)) AS Balance
FROM
Invoices i
WHERE
i.CustomerId = 12
AND i.InvoiceDate BETWEEN '20150601' AND '20160229'
AND i.OutstandingBalance > 0.02
Throws an error telling me that the syntax is incorrect near the Keyword AS (which I presume refers to AS Balance. I suspect that I should probably be 'setting' the value of @OutstandingBalance
but adding a set statement within the select also throws errors.
Is it possible to create a running balance in this sort of query and if so how does one accommodate setting the @OutstandingBalance
to achieve it?
In response to the answer below this is the result set I get:
EDIT Revised query to accommodate both invoices and payments:
SELECT 'Invoice' AS TransactionType,
i.InvoiceNumber AS Description,
i.InvoiceDate AS TransactionDate,
CAST(ROUND(i.OutstandingBalance,2)AS DECIMAL(12,2)) AS TransactionAmount ,
SUM(CAST(ROUND(i.OutstandingBalance,2)AS DECIMAL(12,2))) OVER(ORDER BY i.InvoiceDate, i.InvoiceNumber) AS Balance
FROM Invoices i
WHERE i.CustomerId = 12
AND i.InvoiceDate BETWEEN '20150601' AND '20160229'
AND i.OutstandingBalance > 0.02
UNION
SELECT
'Payment' AS TransactionType,
ip.InvoicePaymentId AS Description,
ip.InvoicePaymentDate AS TransactionDate,
- ip.Amount AS TransactionAmount,
SUM(CAST(ROUND(-ip.Amount,2) AS DECIMAL(12,2))) OVER(ORDER BY ip.InvoicePaymentDate,ip.InvoicePaymentId) AS Balance
FROM InvoicePayments ip
WHERE ip.CustomerId = 12
AND ip.InvoicePaymentDate BETWEEN '20150601' AND '20160229'
ORDER BY TransactionDate, Description
Which produces the following: