I tried looking for some solution here, but still stuck.
WITH MyData AS (
SELECT 'Jun 2024' AS [date], 'A' AS [group], 69 AS Supply, 0 AS Forecast
UNION ALL SELECT 'Jul 2024', 'A', 61, 0
UNION ALL SELECT 'Aug 2024', 'A', 333, 0
UNION ALL SELECT 'Sep 2024', 'A', 57, 21
UNION ALL SELECT 'Oct 2024', 'A', 49, 29
UNION ALL SELECT 'Aug 2024', 'B', 0, 18
UNION ALL SELECT 'Sep 2024', 'B', 0, 287
UNION ALL SELECT 'Oct 2024', 'B', 171, 21
UNION ALL SELECT 'Nov 2024', 'B', 0, 6
)
SELECT
cast (MyData.[date] as DATE) as DATE,
MyData.[group],
MyData.Forecast,
MyData.Supply,
SUM(MyData.Supply - MyData.Forecast) OVER (
PARTITION BY MyData.[group]
ORDER BY cast (MyData.[date] as DATE)
) AS Balance
FROM MyData
ORDER BY MyData.[group], cast (MyData.[date] as DATE);
Here is my Result
enter image description here
I need to calculate a Supportable logic is if Balance >= 0 then Forecast
Note that group B balance all negative, however, i have 171 in Oct => Supportable is 21 and so on.
enter image description here
Please give me some idea how to achieve that