Reputation: 89
I have a table like
Month | Total | Installment 1 | Installment 2 | Installment 3 |
---|---|---|---|---|
Jan 19 | 500 | 100 | 200 | 100 |
Feb 19 | 500 | 100 | 100 | 100 |
Is it possible to subtract the value from the previous columns?
The total for Jan19 is 500. Installment 1 is 100. Balance is 400. Then this 400 should be subtracted with Installment 2 and so on.
I've created a sample output.
Month | Total | Balance 1 | Balance 2 | Balance 3 |
---|---|---|---|---|
Jan 19 | 500 | 400 | 200 | 100 |
Feb 19 | 500 | 400 | 300 | 200 |
Thanks
Upvotes: 0
Views: 35
Reputation: 1270091
Just use -
:
select month, total,
(total - installment1) as balance1,
(total - (installment1 + installment2)) as balance2,
(total - (installment1 + installment2 + installment3)) as balance3
from t;
That said, storing data across columns in a SQL table is not the recommended data model for relational databases. Each installment should be a separate row.
Upvotes: 0