您可以使用类似的东西(来自this):
declare @alo as table(x int, y float);
insert into @alo (x,y) values
(1,10),
(2,20),
(3,null),
(4,null),
(5,null),
(6,30)
;
declare @sumtable as table(sx int ,sy int ,sx2 int,sy2 int ,sxy int, n int );
insert into @sumtable
select
SUM(d.x) as sx,
SUM(d.y) as sy,
SUM(d.x2) as sx2,
SUM(d.y2) as sy2,
SUM(d.xy) as sxy,
count(0) as n
from (
select
x, x*x as x2,
y, y*y as y2,
x*y as xy
from @alo
where x is not null and y is not null
) D
declare @sx int = (select sx from @sumtable),
@sx2 int = (select sx2 from @sumtable),
@sy int= (select sy from @sumtable),
@sy2 int= (select sy2 from @sumtable),
@sxy int= (select sxy from @sumtable),
@n int = (select n from @sumtable);
declare @b as float = cast((@n*@sxy- @sx*@sy) as float)/ cast((@n*@sx2 - @sx*@sx) as float);
declare @a as float = (1.0/@n)*@sy - @b*(1.0/@n)*@sx;
update @alo
set y = @b*x+@a
where y is null
select * from @alo