I don't have much experience with window functions and I have to use one for my average calculation, This is my code:
AVG(b.TotalSilkHrs) OVER(partition BY b.TECHNICIANCODE
ORDER BY b.rankID
ROWS BETWEEN CURRENT ROW and 3 FOLLOWING) AS MovingAvg
I calculate average of sum that was calculated in subquery. It gives me error :
Msg 102, Level 15, State 1, Line 24 Incorrect syntax near 'ROWS'.
Why am I getting error? I used the code I got from your site
Your original syntax works only for SQL Server 2012, for older versions Try this
;WITH AVGCTE AS
(
SELECT *,
ROW_NUMBER() OVER(partition BY TECHNICIANCODE ORDER BY rankID) Rn
FROM TableA
)
SELECT A.*, B.AVG_COL
FROM AVGCTE A
CROSS APPLY
(
SELECT x.AVG(TotalSilkHrs) AVG_COL
FROM AVGCTE x WHERE x.Rn BETWEEN A.rn and a.rn + 2
AND A.TECHNICIANCODE = x.TECHNICIANCODE
) B
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.