New answers tagged row
Assuming that stmnt_date has a UNIQUE constraint, this would be fairly easy with window/analytic functions: SELECT s.stmnt_date, s.debit, s.credit, SUM(s.debit - s.credit) OVER (ORDER BY s.stmnt_date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS balance FROM ...
In, for example, MSSQL: Use a with() statement to generate a CTE. This is essentially a temporary result set which will show the value of each row. You can use math in the with statement to create a column at the end, using math to show the total of the row is DEBIT-CREDIT. In your with statement you'll need to assign row numbers to each row, use the OVER ...
I think you could try the following: set @balance := 0; SELECT stmnt_date, debit, credit, (@balance := @balance + (debit - credit)) as Balance FROM statements ORDER BY stmnt_date;
Top 50 recent answers are included