I need to calculate the running total in which the target column is changing along with the time. Its something which aws does. Charging based for only what you use. So how do I aggregate sum of payments and charges.
Currently I'm storing the running total in the balance column and that sum is calculated using sum of charges - sum of payments.
Here's what the table looks like
Invoices
id tx_type amount live
1 Charge 100 true // live = true means that the amount will be updated and the meter is ticking
2 Payment 100 false
3 Charge 50 true
4 Charge 75 false
5 Payment 125 false
UserBalances
id user_id balance
1 1 0.0
So the balance at this instant will be 0 but after couple of seconds should be different as meter is still ticking. How do I get the sum of amount field which is ever changing?
PS: I'm using ruby on rails and postgresql
Thanks