I would like to get each month balance for those records, with a capping to 500 credits at the beginning of each month.
And I am a bit stuck because I think I can't simply do a rolling sum because the maximum balance for a customer is twice the amount of his new credits (I use 500 as a maximum in the example).
Here is my data :
CREATE TABLE table1 as (
SELECT 'A' as customer_id, 250 as new_credits, -62 as debit, 1 as month_nb
UNION ALL
SELECT 'A', 250, -84, 2
UNION ALL
SELECT 'A', 250, -8, 3
UNION ALL
SELECT 'A', 210, -400, 4
UNION ALL
SELECT 'A', 210, -162, 5
UNION ALL
SELECT 'A', 210, 0, 6
)
I would like to see these result :
Any thought ? Thanks !
