所以我有三列:位置ID,年份和高度。
我想计算五年滚动平均值。但是,如果没有五年的数据,我不想要一个结果。
我一直在学习如何使用OVER。我已经看到了有关此主题的其他问题,但无法找到我的问题的解决方案。
我站在哪里:
select locationID, year_num, height_num2,
avg(cast(height_num2 as float)) over (PARTITION BY locationID
ORDER BY year_num
ROWS 4 PRECEDING) as FiveYearRollingAverage
from combined;
现在我对如何最好地解决这个问题感到难过。
答案 0 :(得分:3)
我认为你必须添加一个计数器,以便知道平均值中涉及的记录数量。如果它们是5,则使用外部查询选择包含滚动平均值的记录:
SELECT locationID, year_num, FiveYearRollingAverage
FROM (
SELECT locationID, year_num,
AVG(CAST(height_num2 AS FLOAT)) OVER (PARTITION BY locationID ORDER BY year_num ROWS 4 PRECEDING) FiveYearRollingAverage,
COUNT(*) OVER (PARTITION BY locationID ORDER BY year_num ROWS 4 PRECEDING) yearsCount
FROM @combined) u
WHERE u.yearsCount = 5
使用此输入:
DECLARE @combined TABLE (locationID INT, year_num INT, height_num2 INT)
INSERT @combined VALUES
(1, 2009, 1),
(1, 2010, 4),
(1, 2011, 3),
(1, 2012, 2),
(1, 2013, 5),
(1, 2014, 7),
(2, 2014, 2),
(2, 2015, 1),
(2, 2016, 4),
(2, 2017, 3)
你得到这个输出:
locationID year_num FiveYearRollingAverage
----------------------------------------------
1 2013 3
1 2014 4,2
locationID = 2没有输出,因为此ID只有4年可用。