I want a query to track interest on a mortgage account. For simplicity assume the interest is calculated yearly. There are also one-off deposits/withdrawals (repayments etc).
I want to query this information and calculate a running balance, presumably using window functions. Here is an example of the kind of table I want to query.
year | changes | interest | comment
2020 | 10000 | 2.5 | initial mortgage of 10k
2021 | 0 | 2.0 | next year the rate drops
2022 | 5000 | 2.0 | we borrow an extra 5k
2023 | 0. | 1.5 | rate drop again
I want a query that calculates the running balance each year, like so:
year | changes | interest | balance
2020 | 10000 | 2.5 | 10250.0 = 10000 * (1 + 2.5 / 100)
2021 | 0 | 2.0 | 10455.0 = 10250 * (1 + 2.0 / 100)
2022 | 5000 | 2.0 | 15764.1 = (10455 + 5000) * (1 + 2.0 / 100)
2023 | 0. | 1.5 | 16000.56 = 15764.1 * (1 + 1.5 / 100)
How to do this in PostgreSQL?