我想在SQL Server 2012中插入缺失值并相应地更新我的表
例如我的数据如下:
Week_Number Var1 Output_Var
1 10 10
2 20 20
3 NULL 22.5
4 NULL 25.0
5 NULL 27.5
7 30 30
var1的输出应该看起来像Output_Var变量。
答案 0 :(得分:3)
declare @alo as table(x int, y float);
insert into @alo (x,y) values (1,10);
insert into @alo (x,y) values (2,20);
insert into @alo (x,y) values (3,null);
insert into @alo (x,y) values (4,null);
insert into @alo (x,y) values (5,null);
insert into @alo (x,y) values (6,30);
SELECT this.x as [X], isnull(this.y,
(
SELECT CASE WHEN next.x IS NULL THEN prev.y
WHEN prev.x IS NULL THEN next.y
WHEN next.x = prev.x THEN prev.y
ELSE prev.y + ( (next.y - prev.y) * (this.x - prev.x) / (next.x - prev.x) )
END
FROM
( SELECT TOP 1 X, Y FROM @alo WHERE x <= this.x and y is not null ORDER BY x DESC ) AS prev
CROSS JOIN
( SELECT TOP 1 X, Y FROM @alo WHERE x >= this.x and y is not null ORDER BY x ASC ) AS next
)) as [Y]
FROM @alo this order by this.x ASC
答案 1 :(得分:0)
您可以使用simple linear regression技术估算缺失值,请参阅“numerical example”。
答案 2 :(得分:0)
您可以使用类似的内容(来自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