Hallo ukulele,
klar. Hier zunächst der Code.
SELECT t.*, max(Kumulierte_Menge) OVER (PARTITION BY [EMATN] ORDER BY WERKS,[TheISOweek],TheISOYear,grp) as new_Value
FROM
(
Select
KalTbl.[Key]
,KalTbl.[MANDT]
,KalTbl.[EMATN]
,KalTbl.[WERKS]
,KalTbl.[TYP]
,KalTbl.[TheISOweek]
,KalTbl.[TheISOYear]
--,WE.FFMNNO
,ISNULL(WE.Menge_WE,0) Menge_WE
,ISNULL(SUM(WE.MENGE_WE)
OVER (PARTITION BY WE.[EMATN] ORDER BY WE.WERKS,WE.[KW],TheISOYear),0) Kumulierte_Menge
,COUNT(WE.MENGE_WE) OVER (PARTITION BY WE.[EMATN] ORDER BY WE.WERKS,[TheISOweek],TheISOYear) as grp
FROM
(
SELECT
[Key]
,[MANDT]
,[EMATN]
,[WERKS]
,[TYP]
--,EBTYP
,TheISOweek
,TheISOYear
FROM
(SELECT DISTINCT
[Key]
,[MANDT]
,[EMATN]
,[WERKS]
,[TYP]
--,EBTYP
FROM
[dbo].[v_LB_Wareneingaenge]
--WHERE [v_LB_Wareneingaenge].EMATN = '000000000000011146' OR [v_LB_Wareneingaenge].EMATN = '000000000000005252'
-- AND [v_LB_Wareneingaenge].WERKS = '1010'
) w
CROSS JOIN
(SELECT DISTINCT
TheIsoweek, TheIsoYear
FROM
[dbo].[DateDimension]
Where TheISOYear between Year(GetDate())-1 and Year(GetDate())+2) a) KalTbl
LEFT JOIN
[dbo].[v_LB_Wareneingaenge_KW] WE
ON WE.MANDT = KalTbl.MANDT
AND WE.EMATN = KalTbl.EMATN
AND WE.WERKS = KalTbl.WERKS
AND RIGHT(WE.KEYDATE,4) = KalTbl.TheISOYear
AND WE.KW = KalTbl.TheISOweek
WHERE CONCAT(KalTbl.TheISOYear,RIGHT(CONCAT('0',KalTbl.[TheISOweek]),2)) >= CONCAT(YEAR(GETDATE()),DATEPART(ISOWK,GETDATE()))
AND (KalTbl.EMATN = '000000000000011146' OR KalTbl.EMATN = '000000000000005252'
AND KalTbl.WERKS = '1010')
--Order by EMATN, KalTbl.TheISOYear, TheISOweek
) t
Order by EMATN, TheISOYear, TheISOweek
Das Ergebnis diese Abfrage ist anbei. Dort ist gut zu erkennen, dass er Ergebnisse für 2023 & 2024 ausgibt, wo ich aber erwartet hätte das er "0" bringt, da er dafür auch kein grp Wert in dem Jahr gebracht hat.