Reputation: 314
I want to get carry forward a field value using another field
This is my table transaction
------------------------------
id | amount | is_income |
------------------------------
1 | 400 |1
2 | 100 |0
3 | 300 |1
I want to get the result like
--------------------------------------
id | amount | is_income |Balance |
--------------------------------------
1 | 400 |1 | 400
2 | 100 |0 | 300
3 | 300 |1 | 600
If is_income field is 1 then add amount with previous amount. and if is_income field is 0 then subtract with previous amount.
if any method to get result without stored procedure ?
Upvotes: 0
Views: 86
Reputation: 4739
Try this:
SET @balance = 0;
SELECT * , IF( is_income =1, @balance := @balance + amount, @balance := @balance - amount ) AS balance FROM `your table name `
Upvotes: 3