WITH tabelle(WERKS,DateID,MATNR,Total_Menge_FERT_ABC,WA_Menge_FERT) AS (
SELECT 1010,20230731,59025,99,8.000 UNION ALL SELECT
1010,20230807,59025,82,17.000 UNION ALL SELECT
1010,20230911,59025,16,12.000 UNION ALL SELECT
1010,20240603,59025,0,0.000 UNION ALL SELECT
1010,20240624,59025,0,0.000 UNION ALL SELECT
1044,20230703,59025,61,3.000 UNION ALL SELECT
1044,20230710,59025,58,3.000 UNION ALL SELECT
1044,20230814,59025,48,0.000 UNION ALL SELECT
1044,20230918,59025,43,2.000 UNION ALL SELECT
1044,20231009,59025,38,1.000 UNION ALL SELECT
1044,20231106,59025,33,1.000 UNION ALL SELECT
1044,20240122,59025,31,0.000 UNION ALL SELECT
1044,20240226,59025,31,0.000 UNION ALL SELECT
1044,20240311,59025,31,0.000 UNION ALL SELECT
1044,20240318,59025,31,0.000 UNION ALL SELECT
1044,20240401,59025,31,0.000 UNION ALL SELECT
1044,20240422,59025,31,0.000 UNION ALL SELECT
1044,20240527,59025,31,0.000 UNION ALL SELECT
1044,20240603,59025,31,0.000 UNION ALL SELECT
1044,20240610,59025,31,0.000 UNION ALL SELECT
1010,20230626,94074,1,0.000 UNION ALL SELECT
1010,20230717,94074,1,0.000 UNION ALL SELECT
1010,20230724,94074,1,0.000 UNION ALL SELECT
1010,20230807,94074,1,1.000 UNION ALL SELECT
1010,20230821,94074,0,0.000 UNION ALL SELECT
1010,20231211,94074,0,0.000 UNION ALL SELECT
1010,20240122,94074,0,0.000 UNION ALL SELECT
1010,20240408,94074,0,0.000 UNION ALL SELECT
1010,20240624,94074,0,0.000 UNION ALL SELECT
1010,20230814,59025,62,20.000 UNION ALL SELECT
1010,20231002,59025,0,11.000 UNION ALL SELECT
1010,20240108,59025,0,0.000 UNION ALL SELECT
1010,20240212,59025,0,0.000 UNION ALL SELECT
1010,20240318,59025,0,0.000 UNION ALL SELECT
1010,20240527,59025,0,0.000 UNION ALL SELECT
1010,20240617,59025,0,0.000 UNION ALL SELECT
1044,20230731,59025,48,4.000 UNION ALL SELECT
1044,20230904,59025,46,1.000 UNION ALL SELECT
1044,20231002,59025,39,2.000 UNION ALL SELECT
1044,20231113,59025,32,1.000 UNION ALL SELECT
1044,20231120,59025,32,0.000 UNION ALL SELECT
1044,20231204,59025,32,0.000 UNION ALL SELECT
1044,20240108,59025,31,0.000 UNION ALL SELECT
1044,20240115,59025,31,0.000 UNION ALL SELECT
1044,20240415,59025,31,0.000 UNION ALL SELECT
1044,20240506,59025,31,0.000 UNION ALL SELECT
1010,20231023,94074,0,0.000 UNION ALL SELECT
1010,20231113,94074,0,1.000 UNION ALL SELECT
1010,20231127,94074,0,1.000 UNION ALL SELECT
1010,20231204,94074,0,0.000 UNION ALL SELECT
1010,20240108,94074,0,0.000 UNION ALL SELECT
1010,20240129,94074,0,0.000 UNION ALL SELECT
1010,20240226,94074,0,0.000 UNION ALL SELECT
1010,20240401,94074,0,0.000 UNION ALL SELECT
1010,20240415,94074,0,0.000 UNION ALL SELECT
1010,20230626,59025,23,11.000 UNION ALL SELECT
1010,20230703,59025,133,10.000 UNION ALL SELECT
1010,20230710,59025,124,9.000 UNION ALL SELECT
1010,20230828,59025,44,19.000 UNION ALL SELECT
1010,20230925,59025,0,12.000 UNION ALL SELECT
1010,20231106,59025,0,6.000 UNION ALL SELECT
1010,20231113,59025,0,6.000 UNION ALL SELECT
1010,20231211,59025,0,4.000 UNION ALL SELECT
1010,20231225,59025,0,4.000 UNION ALL SELECT
1010,20240226,59025,0,0.000 UNION ALL SELECT
1010,20240325,59025,0,0.000 UNION ALL SELECT
1010,20240401,59025,0,0.000 UNION ALL SELECT
1010,20240415,59025,0,0.000 UNION ALL SELECT
1010,20240422,59025,0,0.000 UNION ALL SELECT
1044,20230626,59025,39,2.000 UNION ALL SELECT
1044,20230828,59025,47,1.000 UNION ALL SELECT
1044,20231023,59025,35,2.000 UNION ALL SELECT
1044,20231218,59025,32,0.000 UNION ALL SELECT
1044,20240212,59025,31,0.000 UNION ALL SELECT
1044,20240304,59025,31,0.000 UNION ALL SELECT
1010,20230710,94074,1,0.000 UNION ALL SELECT
1010,20230731,94074,1,0.000 UNION ALL SELECT
1010,20230828,94074,0,0.000 UNION ALL SELECT
1010,20231009,94074,1,1.000 UNION ALL SELECT
1010,20231016,94074,1,1.000 UNION ALL SELECT
1010,20240520,94074,0,0.000 UNION ALL SELECT
1010,20240603,94074,0,0.000 UNION ALL SELECT
1010,20230717,59025,115,9.000 UNION ALL SELECT
1010,20230821,59025,63,19.000 UNION ALL SELECT
1010,20230918,59025,3,13.000 UNION ALL SELECT
1010,20231016,59025,0,4.000 UNION ALL SELECT
1010,20231023,59025,0,5.000 UNION ALL SELECT
1010,20231030,59025,0,5.000 UNION ALL SELECT
1010,20231218,59025,0,3.000 UNION ALL SELECT
1010,20240115,59025,0,0.000 UNION ALL SELECT
1010,20240122,59025,0,0.000 UNION ALL SELECT
1010,20240408,59025,0,0.000 UNION ALL SELECT
1044,20230807,59025,48,0.000 UNION ALL SELECT
1044,20230821,59025,48,0.000 UNION ALL SELECT
1044,20230911,59025,45,1.000 UNION ALL SELECT
1044,20230925,59025,41,2.000 UNION ALL SELECT
1044,20231016,59025,37,1.000 UNION ALL SELECT
1044,20231030,59025,34,1.000 UNION ALL SELECT
1044,20231211,59025,32,0.000 UNION ALL SELECT
1044,20240101,59025,31,1.000 UNION ALL SELECT
1044,20240129,59025,31,0.000 UNION ALL SELECT
1010,20230703,94074,1,0.000 UNION ALL SELECT
1010,20230911,94074,0,0.000 UNION ALL SELECT
1010,20230918,94074,3,0.000 UNION ALL SELECT
1010,20230925,94074,0,0.000 UNION ALL SELECT
1010,20231002,94074,0,0.000 UNION ALL SELECT
1010,20231218,94074,0,0.000 UNION ALL SELECT
1010,20231225,94074,0,0.000 UNION ALL SELECT
1010,20240219,94074,0,0.000 UNION ALL SELECT
1010,20240304,94074,0,0.000 UNION ALL SELECT
1010,20240325,94074,0,0.000 UNION ALL SELECT
1010,20240429,94074,0,0.000 UNION ALL SELECT
1010,20231009,59025,0,4.000 UNION ALL SELECT
1010,20231120,59025,0,7.000 UNION ALL SELECT
1010,20231127,59025,0,6.000 UNION ALL SELECT
1010,20240129,59025,0,0.000 UNION ALL SELECT
1010,20240205,59025,0,0.000 UNION ALL SELECT
1010,20240219,59025,0,0.000 UNION ALL SELECT
1010,20240304,59025,0,0.000 UNION ALL SELECT
1010,20240429,59025,0,0.000 UNION ALL SELECT
1010,20240513,59025,0,0.000 UNION ALL SELECT
1044,20230717,59025,55,3.000 UNION ALL SELECT
1044,20231225,59025,32,0.000 UNION ALL SELECT
1044,20240205,59025,31,0.000 UNION ALL SELECT
1044,20240408,59025,31,0.000 UNION ALL SELECT
1010,20231030,94074,0,1.000 UNION ALL SELECT
1010,20231106,94074,0,0.000 UNION ALL SELECT
1010,20240101,94074,0,0.000 UNION ALL SELECT
1010,20240115,94074,0,0.000 UNION ALL SELECT
1010,20240311,94074,0,0.000 UNION ALL SELECT
1010,20240318,94074,0,0.000 UNION ALL SELECT
1010,20240506,94074,0,0.000 UNION ALL SELECT
1010,20240513,94074,0,0.000 UNION ALL SELECT
1010,20240527,94074,0,0.000 UNION ALL SELECT
1010,20240610,94074,0,0.000 UNION ALL SELECT
1010,20240617,94074,0,0.000 UNION ALL SELECT
1010,20230724,59025,107,8.000 UNION ALL SELECT
1010,20230904,59025,28,16.000 UNION ALL SELECT
1010,20231204,59025,0,5.000 UNION ALL SELECT
1010,20240101,59025,0,3.000 UNION ALL SELECT
1010,20240311,59025,0,0.000 UNION ALL SELECT
1010,20240506,59025,0,0.000 UNION ALL SELECT
1010,20240520,59025,0,0.000 UNION ALL SELECT
1010,20240610,59025,0,0.000 UNION ALL SELECT
1044,20230724,59025,52,3.000 UNION ALL SELECT
1044,20231127,59025,32,0.000 UNION ALL SELECT
1044,20240219,59025,31,0.000 UNION ALL SELECT
1044,20240325,59025,31,0.000 UNION ALL SELECT
1044,20240429,59025,31,0.000 UNION ALL SELECT
1044,20240513,59025,31,0.000 UNION ALL SELECT
1044,20240520,59025,31,0.000 UNION ALL SELECT
1044,20240617,59025,31,0.000 UNION ALL SELECT
1044,20240624,59025,31,0.000 UNION ALL SELECT
1010,20230814,94074,0,0.000 UNION ALL SELECT
1010,20230904,94074,0,0.000 UNION ALL SELECT
1010,20231120,94074,0,0.000 UNION ALL SELECT
1010,20240205,94074,0,0.000 UNION ALL SELECT
1010,20240212,94074,0,0.000 UNION ALL SELECT
1010,20240422,94074,0,0.000
), t0 AS (
--t0 dient einzig und allein dazu aus DateID ein brauchbares Datum zu machen
SELECT WERKS,
DateID,
dateadd(day,convert(INT,right(convert(CHAR(8),DateID),2))-1,
dateadd(month,convert(INT,right(left(convert(CHAR(8),DateID),6),2))-1,
dateadd(year,convert(INT,left(convert(CHAR(8),DateID),4))-1900,0)
)) AS [Date],
MATNR,
cast(Total_Menge_FERT_ABC AS INT) AS Total_Menge_FERT_ABC,
cast(WA_Menge_FERT AS INT) AS WA_Menge_FERT
FROM tabelle
), t1 AS (
--Basis vom CTE
SELECT DateID,
[Date],
Total_Menge_FERT_ABC,
WERKS,
MATNR,
WA_Menge_FERT,
Total_Menge_FERT_ABC - WA_Menge_FERT AS rest, --*hier war es vorher ohne - WA_Menge_FERT
1 AS [count]
FROM t0
UNION ALL
--Rekursion vom CTE
SELECT t1.DateID,
t1.[Date],
t1.Total_Menge_FERT_ABC,
t1.WERKS,
t1.MATNR,
t1.WA_Menge_FERT,
t1.rest - t0.WA_Menge_FERT AS rest,
t1.[count] + 1 AS [count]
FROM t1
INNER JOIN t0
ON t1.WERKS = t0.WERKS
AND t1.MATNR = t0.MATNR
AND dateadd(day,t1.[count] * 7,t1.[Date]) = t0.[Date]
--der max(count) ist später interessant, also beim letzten Datensatz count = gesuchter Wert
WHERE t1.rest > 0
--bei count = 99 dauert es dann 99 Wochen oder länger, hier wird eine Endlosrekursion verhindert und die Performance kann verbessert werden in dem Härtefälle nicht weiter durchgerechnet werden
AND t1.[count] + 1 <= 99
), t2 AS (
SELECT ROW_NUMBER() OVER (PARTITION BY WERKS, MATNR, [Date] ORDER BY [count] DESC) AS zeile,
DateID,
[Date],
Total_Menge_FERT_ABC,
WERKS,
MATNR,
WA_Menge_FERT,
rest,
[count]
FROM t1
)
SELECT DateID,
[Date],
Total_Menge_FERT_ABC,
WERKS,
MATNR,
WA_Menge_FERT,
rest,
(CASE WHEN rest <= 0 THEN [count] ELSE NULL END) AS gesuchter_wert
FROM t2
WHERE zeile = 1
ORDER BY WERKS, MATNR, [Date], [count]