select ROW_NUMBER() OVER (ORDER BY [A] asc) AS id,A,B,C,0 as E into #temp from dbo.rawdata
order by id asc
-------------------------
update #temp
set E=((select max(h.B) from(select top (9) b.B from #temp b where b.id<#temp.id order by b.id asc)h) + (select min(l.C) from(select top (9) c.C from #temp c where c.id<#temp.id order by id asc)l))/2
where id>10
答案 0 :(得分:1)
这可能是一种更有效的方法,但这里有一种使用OUTER APPLY
的方法:
;With Cte As
(
Select *,
Row_Number() Over (Order By A) As RN
From YourTable
)
Select T1.A, T1.B, T1.C, T1.D, X.E
From Cte T1
Outer Apply
(
Select (Max(T2.B) + Min(T2.C)) / 2 As E
From Cte T2
Where T2.RN >= (T1.RN - 9)
And T2.RN < T1.RN
And T1.RN >= 10
) X
使用这些值UPDATE
您的表格,您可以使用以下内容:
;With Cte As
(
Select *,
Row_Number() Over (Order By A) As RN
From YourTable
)
Update T1
Set E = X.E
From Cte T1
Outer Apply
(
Select (Max(T2.B) + Min(T2.C)) / 2 As E
From Cte T2
Where T2.RN >= (T1.RN - 9)
And T2.RN < T1.RN
And T1.RN >= 10
) X