MySQL Forums
Forum List  »  General

running balance
Posted by: Oliver Mercado
Date: March 18, 2019 07:57AM

Hi,

I am seeking help on how to display the following.

Total Amount = 260

+----+-------+--------+---------+
| id | debit | credit | balance |
+----+-------+--------+---------+
| 1 | NULL | 35 | NULL |
| 2 | 65 | NULL | 225 |
| 3 | NULL | 35 | NULL |
| 4 | 65 | NULL | 190 |
| 5 | 65 | NULL | 190 |
| 6 | 65 | NULL | 190 |
+----+-------+--------+---------+

I tried the query below but I don't get what I want

"SELECT id
, debit
, credit
, @balance := @balance+(COALESCE(debit,credit*-1)) balance
FROM my_table
, (SELECT @balance :=0) vars
ORDER
BY id;"

+----+-------+--------+---------+
| id | debit | credit | balance |
+----+-------+--------+---------+
| 1 | NULL | 35 | -35 |
| 2 | 65 | NULL | 30 |
| 3 | NULL | 35 | -5 |
| 4 | 65 | NULL | 60 |
| 5 | 65 | NULL | 125 |
| 6 | 65 | NULL | 190 |
+----+-------+--------+---------+

Options: ReplyQuote


Subject
Written By
Posted
running balance
March 18, 2019 07:57AM
March 19, 2019 10:42AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.