[ad_1]
Have a requirement where I would need to rope the calculated value of the previous row for calculation in the current row.
The following is a sample of how the data currently looks :-
ID | Date | Days |
---|---|---|
1 | 2022-01-15 | 30 |
2 | 2022-02-18 | 30 |
3 | 2022-03-15 | 90 |
4 | 2022-05-15 | 30 |
The following is the output What I am expecting :-
ID | Date | Days | CalVal |
---|---|---|---|
1 | 2022-01-15 | 30 | 2022-02-14 |
2 | 2022-02-18 | 30 | 2022-03-16 |
3 | 2022-03-15 | 90 | 2022-06-14 |
4 | 2022-05-15 | 30 | 2022-07-14 |
The value of CalVal for the first row is Date + Days
From the second row onwards it should take the CalVal value of the previous row and add it with the current row Days
Is there anyway we can achieve the above via Postgres SQL? I have been tinkering with window functions and even recursive CTEs but have had no luck 🙁
Would appreciate any direction!
Thanks in advance!
[ad_2]