代码 :
SELECT YEAR_ID, Count, ThreeYEARAverage,
AVG(Count) OVER (ORDER BY YEAR_ID ROWS 4 PRECEDING) AS FiveYEARAverage
FROM (SELECT YEAR_ID, ISNULL(Count, 0) AS Count,
AVG(ISNULL(Count, 0)) OVER (ORDER BY YEAR_ID ROWS 2 PRECEDING) AS ThreeYEARAverage
FROM (SELECT DATEPART(YEAR, Date_plan) AS YEAR_ID,
SUM(ISNULL(Amount_produced, 0)) AS Count
FROM History_mango
WHERE DATEPART(YEAR, Date_plan) BETWEEN '2012' AND '2016'
AND DATEPART(month, Date_plan) = '09'
GROUP BY DATEPART(YEAR, Date_plan)) AS id
) AS id1
结果
YEAR_ID Count ThreeYEARAverage FiveYEARAverage
2012 22 22.000000 22.000000
2013 555 288.500000 288.500000
2014 100 225.666666 225.666666
2015 88 247.666666 191.250000
2016 200 129.333333 193.000000
需要
YEAR_ID Count ThreeYEARAverage FiveYEARAverage
2012 22
2013 555
2014 100 225.666666
2015 88 247.666666
2016 200 129.333333 193.000000
谢谢你亲爱的