;WITH baseTable
as
(
select Datum, MainIndex, Stand, UserName, ROW_NUMBER() OVER (ORDER BY datum ASC) AS RowNo
from tblIndexTmp
),
t3 as
(
SELECT b1.Datum, b1.MainIndex, b1.Stand, b1.UserName, AVG(b2.Stand) AvgStand
FROM baseTable b1
INNER JOIN baseTable b2 ON b1.UserName = b2.UserName AND b1.MainIndex = b2.MainIndex AND b2.RowNo BETWEEN b1.RowNo-199 AND b1.RowNo
WHERE b1.UserName = @username
GROUP BY b1.Datum, b1.MainIndex, b1.Stand, b1.UserName
)
update t1
set t1.Variable_1 = t3.AvgStand
from tblIndexTmp t1
INNER JOIN t3 ON t1.UserName = t3.UserName AND t1.MainIndex = t3.MainIndex AND t1.Datum = t3.Datum
|