I have the below table
ID | Week_No | Value | Available_Stock |
---|---|---|---|
1 | 1 | 200 | 1000 |
1 | 2 | 300 | 1000 |
1 | 3 | 100 | 1000 |
1 | 4 | 400 | 1000 |
1 | 5 | 500 | 1000 |
I want a table like this
ID | SUM(VALUE) | Stock_Lasts_Weeks | Available_Stock |
---|---|---|---|
1 | 800 | 4 Weeks | 1000 |
As my Available_Stock is 1000 and on the 5th week my Value exceeds 1000 my stock would lasts for 4 weeks only. I would like to calculate this on Snowflake. I will have to add the value and check if the value exceeds the available_stock then the previous_week before exceeding should be chosen.