【问题标题】:SQL Server Interpolate Missing rowsSQL Server 插入缺失的行
【发布时间】:2010-06-10 20:13:13
【问题描述】:

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

  1. 归还缺失的日子
  2. 使用线性插值计算缺失值

所以来自下面的源表:

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

任何帮助将不胜感激。

【问题讨论】:

    标签: tsql missing-data linear-interpolation


    【解决方案1】:
    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
    

    【讨论】:

    • 感谢 GalacticJello 你是明星。正是我所追求的。
    • 此解决方案根据表中的第一个和最后一个条目计算因子的缺失值。我稍微修改了代码,以根据任何给定行的前一个和下一个已知值重新计算缺失值。在行之后:“其中 PID = @thisId + 1”添加行:“select @diff = DATEDIFF(d, @thisDate, @nextDate)” “set @increment = (@nextValue - @thisValue) / @diff”
    猜你喜欢
    • 1970-01-01
    • 2021-11-20
    • 2018-09-21
    • 2020-01-15
    • 1970-01-01
    • 2014-04-14
    • 1970-01-01
    • 2010-10-29
    • 2016-01-09
    相关资源
    最近更新 更多