Let' say I have the following table where percent_leave is the percentage of people from the previous period that leave in the current period:
| Period | percent_leave |
----------------------
| 1 | 0.05 |
| 2 | 0.05 |
| 3 | 0.05 |
| 4 | 0.05 |
I want to calculate a new column that will contain the percentage of people left at the end of that period. For example, if we start with 100 people, 5 people leave in the first period, therefore we are left with 95. 5% of 95 would leave in the second period, which leaves us with 90.25 people, and so forth. Then the table would look like:
| Period | percent_leave | percent_remaining
-----------------------------------------
| 1 | 0.05 | 0.95
| 2 | 0.05 | 0.9025
| 3 | 0.05 | 0.857375
| 4 | 0.05 | 0.81450625
As you can see, the calculation of a new row in percent_remaining refers to the previous row in percent_remaining. Normally I would export this raw data into Excel and do this calculation there, but I would like to automate this task in SQL so I need to figure out how to do this in Postgres.
Any ideas?