I have an installment table as shown below and I would like to get the balance amount based on the amount paid by the client. For e.g. for the first installment it was fully paid, second installment he still owes $10 but will be covered first by the third payment and hence the balance amount will be $30 on the third payment. How can I go about doing this in MySQL?
installment table:
acc_name | installment_amt | installment_no | paid_amt |
---|---|---|---|
A | 100 | 1 | 100 |
A | 100 | 2 | 90 |
A | 100 | 3 | 80 |
A | 100 | 4 | |
A | 100 | 5 |
resulting_table:
acc_name | installment_amt | installment_no | paid_amt | balance |
---|---|---|---|---|
A | 100 | 1 | 100 | 0 |
A | 200 | 2 | 190 | 0 |
A | 300 | 3 | 270 | 30 |
A | 400 | 4 | ||
A | 500 | 5 |
Another scenario would be the third payment is unable to cover fully both the 2nd and third installment as such
installment table:
acc_name | installment_amt | installment_no | paid_amt |
---|---|---|---|
A | 100 | 1 | 100 |
A | 100 | 2 | 90 |
A | 100 | 3 | 5 |
A | 100 | 4 | |
A | 100 | 5 |
resulting_table:
acc_name | installment_amt | installment_no | paid_amt | balance |
---|---|---|---|---|
A | 100 | 1 | 100 | 0 |
A | 200 | 2 | 190 | 5 |
A | 300 | 3 | 195 | 100 |
A | 400 | 4 | ||
A | 500 | 5 |