也许不是最有效的,但应该工作:
WITH cte
AS (SELECT [Creation Date],
Data,
rn=Row_number() OVER(ORDER BY [Creation Date])
FROM dbo.Table)
UPDATE cte
SET Data = ( ( (SELECT c2.Data
FROM cte c2
WHERE c2.rn = cte.rn - 1)
+ (SELECT c2.Data
FROM cte c2
WHERE c2.rn = cte.rn + 1) ) / 2 )
WHERE Data = 0;
我Row_Number
在 CTE 中使用以获取按 . 排序的连续数字Creation Date
。然后这个数字用于根据它的前一个值和下一个值来获取新的数据。
这是一个具有类似架构的演示(我使用了 anint
而不是datetime
):
更新
不错的一个,但它不能处理多个 0 的间隙
很好,这是考虑到这一点的修改后的 sql:
WITH cte
AS (SELECT [Creation Date],
Data,
rn=Row_number() OVER(ORDER BY [Creation Date])
FROM dbo.Table)
UPDATE cte
SET Data = ( ( (SELECT c2.Data
FROM cte c2
WHERE c2.rn = (SELECT MAX(RN)FROM CTE c3 WHERE c3.RN<cte.RN AND c3.Data<>0))
+ (SELECT c2.Data
FROM cte c2
WHERE c2.rn = (SELECT MIN(RN)FROM CTE c3 WHERE c3.RN>cte.RN AND c3.Data<>0))) / 2 )
WHERE Data = 0;
演示(5,6 上有连续的零)