I have a table of data like this:
PeriodYearMonth Reg PartNo ComponentRemovals RunningRemovals
2019 10 G-NHVP 109-0740V01-137 1 1
2019 11 G-NHVP 109-0740V01-137 1 2
2019 12 G-NHVP 109-0740V01-137 1 3
2020 01 G-NHVP 109-0740V01-137 1 4
2019 10 OO-NSF 11-13354P 1 1
2019 09 G-NHVR 11-13354P 2 2
2019 10 OY-HMV 11-13354P 1 1
In the last column I calculated the 7-month running sum of removals per PartNo and per Reg. To do this I made the following code:
/****** Find Running Component Removals ******/
SELECT [PeriodYearMonth]
,[Reg]
,[PartNo]
,[ComponentRemovals]
,sum(sum(ComponentRemovals)) over (Partition by [Reg], PartNo Order By PeriodYearMonth, PeriodYearMonth rows between 6 preceding and current row) as RunningRemovals
,[ConfirmedFailures]
, sum(sum(ConfirmedFailures)) over (Partition by [Reg], PartNo Order By PeriodYearMonth, PeriodYearMonth rows between 6 preceding and current row) as RunningFailures
FROM [RALNHVTST].[dbo].[vtRelRepComponentsRemovalsByPartNo]
Group by Reg, PartNo, ComponentRemovals, ConfirmedFailures, PeriodYearMonth
Order by PartNo
However, since not all months are included in the PeriodYearMonth column the results are incorrect. I've seen solutions online for simple cases, but the tricky part for me is, that I need an entry for every PartNo, every Reg and every month.
Any help would be greatly appreciated.
Regards