declare @t1 table(x float,y float)
insert into @t1(x,y)
(
select 0 as x,0.164 as y
union all select 0.0393409,0.210
union all select 0.0786818,0.256
union all select 0.1180227,0.304
union all select 0.1573636,0.356
union all select 0.1967045,0.399
)
declare @u_x float --x的平均值
declare @u_y float --y的平均值
declare @s2_x float--X方差
declare @cov_x_y float --协方差
--############### 求平均值 ############
select @u_x= avg(x),@u_y=avg(y)
from @t1
--############### 求平均值 ############
--############### 协方差,X方差 ############
select @cov_x_y=sum((x-@u_x)*(y-@u_y))/(select count(1) from @t1),@s2_x=varp(x)--sum (power((x-@u_x),2))
from @t1
--############### 协方差,X方差 ############
declare @a float,@b float
set @a=@cov_x_y/@s2_x
set @b=@u_y-@a*@u_x
print 'y='+cast(@a as varchar(max))+'*x + '+cast(@b as varchar(max))