попробуйте это:
DECLARE @YourTable table (MonthOf datetime, TotalOf numeric(9,4), Impact numeric(9,4), forecast numeric(9,4))
INSERT @YourTable VALUES('2010-04-01', 45792.0000, 1.0000, NULL)
INSERT @YourTable VALUES('2010-05-01', 51789.0000, 1.0000, NULL)
INSERT @YourTable VALUES('2010-06-01', 58228.0000, 1.0000, NULL)
INSERT @YourTable VALUES('2010-07-01', 52956.5217, 1.0000, NULL)
INSERT @YourTable VALUES('2010-08-01', 53600.4700, 0.8810, NULL)
INSERT @YourTable VALUES('2010-09-01', 54257.8784, 1.1838, NULL)
INSERT @YourTable VALUES('2010-10-01', 55134.0669, 1.0000, NULL)
;WITH MonthValues AS
(
SELECT
DATEADD(month,DATEDIFF(month,0,MonthOf),0) AS MonthYear
,SUM(TotalOf) TotalOf
FROM @YourTable
GROUP BY DATEADD(month,DATEDIFF(month,0,MonthOf),0)
)
UPDATE y
SET Forecast=CASE
WHEN Impact = 1.0000 then m1.TotalOf * Impact
WHEN Impact < 1.0000 then m2.TotalOf * Impact
WHEN Impact > 1.0000 then m3.TotalOf * Impact
END
FROM @YourTable y
LEFT OUTER JOIN MonthValues m1 ON DATEADD(month,DATEDIFF(month,0,y.MonthOf),0)=m1.MonthYear
LEFT OUTER JOIN MonthValues m2 ON DATEADD(month,-1,DATEADD(month,DATEDIFF(month,0,y.MonthOf),0))=m2.MonthYear
LEFT OUTER JOIN MonthValues m3 ON DATEADD(month,-2,DATEADD(month,DATEDIFF(month,0,y.MonthOf),0))=m3.MonthYear
SELECT * FROM @YourTable
ВЫВОД:
MonthOf TotalOf Impact forecast
----------------------- ----------- ------- -----------
2010-04-01 00:00:00.000 45792.0000 1.0000 45792.0000
2010-05-01 00:00:00.000 51789.0000 1.0000 51789.0000
2010-06-01 00:00:00.000 58228.0000 1.0000 58228.0000
2010-07-01 00:00:00.000 52956.5217 1.0000 52956.5217
2010-08-01 00:00:00.000 53600.4700 0.8810 46654.6956
2010-09-01 00:00:00.000 54257.8784 1.1838 62689.9304
2010-10-01 00:00:00.000 55134.0669 1.0000 55134.0669
(7 row(s) affected)