Tsql SQL Server插入缺少的行

Tsql SQL Server插入缺少的行,tsql,missing-data,linear-interpolation,Tsql,Missing Data,Linear Interpolation,下表记录了每天的值。问题是有时几天都不见了。我想编写一个SQL查询,它将: 归还失去的日子 使用线性插值计算缺失值 因此,从以下源表: Date Value -------------------- 2010/01/10 10 2010/01/11 15 2010/01/13 25 2010/01/16 40 我想返回: Date Value -------------------- 2010/01/10 1

下表记录了每天的值。问题是有时几天都不见了。我想编写一个SQL查询,它将:

  • 归还失去的日子
  • 使用线性插值计算缺失值
  • 因此,从以下源表:

    Date           Value
    --------------------
    2010/01/10     10
    2010/01/11     15
    2010/01/13     25
    2010/01/16     40
    
    我想返回:

     Date           Value
     --------------------
     2010/01/10     10
     2010/01/11     15
     2010/01/12     20
     2010/01/13     25
     2010/01/14     30
     2010/01/15     35
     2010/01/16     40
    
    任何帮助都将不胜感激。

    declare@MaxDate
    
    declare @MaxDate date
    declare @MinDate date
    
    select @MaxDate = MAX([Date]),
            @MinDate = MIN([Date])
    from Dates
    
    declare @MaxValue int
    declare @MinValue int
    
    select @MaxValue = [Value] from Dates where [Date] = @MaxDate
    select @MinValue = [Value] from Dates where [Date] = @MinDate
    
    declare @diff int
    select @diff = DATEDIFF(d, @MinDate, @MaxDate)
    
    declare @increment int
    set @increment = (@MaxValue - @MinValue)  / @diff
    
    select @increment
    
    declare @jaggedDates as table
    (
        PID INT IDENTITY(1,1) PRIMARY KEY,
        ThisDate date,
        ThisValue int
    )
    
    declare @finalDates as table
    (
        PID INT IDENTITY(1,1) PRIMARY KEY,
        [Date] date,
        Value int
    )
    
    declare @thisDate date
    declare @thisValue int
    declare @nextDate date
    declare @nextValue int
    
    declare @count int
    insert @jaggedDates select [Date], [Value] from Dates
    select @count = @@ROWCOUNT
    
    declare @thisId int 
    set @thisId = 1
    declare @entryDiff int
    declare @missingDate date
    declare @missingValue int
    
    while @thisId <= @count
    begin
        select @thisDate = ThisDate,
                @thisValue = ThisValue
        from @jaggedDates
        where PID = @thisId
    
        insert @finalDates values (@thisDate, @thisValue)
    
        if @thisId < @count
        begin
            select @nextDate = ThisDate,
                @nextValue = ThisValue
            from @jaggedDates
            where PID = @thisId + 1
    
            select @entryDiff = DATEDIFF(d, @thisDate, @nextDate)
            if  @entryDiff > 1
            begin
                set @missingDate = @thisDate
                set @missingValue = @thisValue
                while @entryDiff > 1
                begin
                    set @missingDate = DATEADD(d, 1, @missingDate)
                    set @missingValue = @missingValue + @increment
                    insert @finalDates values (@missingDate, @missingValue)
                    set @entryDiff = @entryDiff - 1
                end
            end
        end
    
        set @thisId = @thisId + 1
    end
    
    select * from @finalDates
    
    声明@MinDate日期 选择@MaxDate=MAX([Date]), @MinDate=MIN([日期]) 从日期开始 声明@MaxValue int 声明@MinValue int 从日期中选择@MaxValue=[Value],其中[Date]=@MaxDate 从日期中选择@MinValue=[Value],其中[Date]=@MinDate 声明@diff int 选择@diff=DATEDIFF(d、@MinDate、@MaxDate) 声明@increment int 设置@increment=(@MaxValue-@MinValue)/@diff 选择@increment 将@jaggedDates声明为表 ( PID INT标识(1,1)主键, 这个日期, 此值整型 ) 将@finalDates声明为表 ( PID INT标识(1,1)主键, [日期]日期, 值int ) 声明@thisDate日期 声明@thisValue int 声明@nextDate日期 声明@nextValue int 声明@countint 插入@jaggedDates从日期中选择[日期],[值] 选择@count=@@ROWCOUNT 声明@thisId int 设置@thisId=1 声明@entryDiff int 声明@missingDate日期 声明@missingValue int 而@thisid1 开始 设置@missingDate=@thisDate 设置@missingValue=@thisValue 而@entryDiff>1 开始 设置@missingDate=DATEADD(d,1,@missingDate) 设置@missingValue=@missingValue+@增量 插入@finalDates值(@missingDate,@missingValue) 设置@entryDiff=@entryDiff-1 结束 结束 结束 设置@thisId=@thisId+1 结束 从@finalDates中选择*
    谢谢银河果冻,你是个明星。这个解决方案基于表中的第一个和最后一个条目计算因子上缺少的值。我稍微修改了代码,以基于任何给定行上的上一个和下一个已知值重新计算缺少的值。在“其中PID=@thisId+1”行之后添加以下行:“选择@diff=DATEDIFF(d,@thisDate,@nextDate)”“设置@increment=(@nextValue-@thisValue)/@diff”