Perhaps not the most efficient, but should work:
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;
I use Row_Number in CTE to get sequential numbers sorted by Creation Date . Then this number is used to obtain new data in accordance with their previous and next value.
Here is a demo with a similar scheme (I used int instead of datetime )
Refresh
Nice, but it doesn't handle spaces with multiple 0
Good catch, here is a modified sql that takes this into account:
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;
Demo (with 5.6 consecutive zeros)
Tim schmelter
source share