Följande kommer att generera dina löpande totala kolumner.
Exempel
Select *
,Running12 = sum(Value) over (Partition By ID Order By Date Rows Between 11 Preceding and Current Row)
,CalendarYTD = sum(Value) over (Partition By ID,Year(Date) Order By Date)
,PrevCalendarYTD = case when month(date)<>1 then null else (Select Value from @YourTable Where ID=A.ID and date=dateadd(year,-1,A.date)) end
From @YourTable A
Order By ID,Date
Returer