I have this table with this sample data:
DECLARE @Sample TABLE (fill_date DATETIME, days_supplied INT)
INSERT INTO @Sample (fill_date, days_supplied)
VALUES (
'02/17/2005', --> DATEADD(dd, 500, '02/17/2005') = '07/02/2006'
500
),
(
'06/13/2005', --> DATEADD(dd, 30, '07/02/2006') = '08/01/2006'
30
),
(
'08/11/2005', --> DATEADD(dd, 30, '08/01/2006') = '08/31/2006'
30
)
I need to add days_supplied to fill_date in the first row, which would be 2006-07-02. If the result is higher than fill_date in the next row i would need to add days_supplies of the next row to the sum of the days_supplied and fill_date of the previous row. If the result of the first row isnt higher than fill_date of the next row then i would need to add days_supplied and fill_date of the next row, and so on.
This is the result i need in the end:
last_fill_date: '08/31/2006'.
What would be the best way to achieve this? Any help is appreciated