0

我想在 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 变量。

4

3 回答 3

4
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
于 2015-12-02T08:59:00.637 回答
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
于 2013-04-15T14:43:40.250 回答
0

您可以使用简单的线性回归技术估计您的缺失值,请参阅“数值示例”。

于 2013-04-15T11:48:25.737 回答