不清楚您是否只想在每个 ShortName 内或整个日期范围内进行插值。这是更简单的日期范围,但如果您也想按 ShortName 进行分区,请回复。
declare @Table table ([Date] datetime, ShortName varchar(100), LongName varchar(100), Value decimal(10,2));
insert into @Table
select '12/31/2012','ABC','Test1','-4.0' union all
select '12/31/2012','XYZ','Test2','-8.1' union all
select '1/2/2013','ABC','Test1',NULL union all
select '1/2/2013','XYZ','Test2',NULL union all
select '1/3/2013','ABC','Test1',NULL union all
select '1/3/2013','XYZ','Test2',NULL union all
select '1/4/2013','ABC','Test1','-9.6' union all
select '1/4/2013','XYZ','Test2','-13.0' union all
select '1/7/2013','ABC','Test1',NULL union all
select '1/7/2013','XYZ','Test2',NULL union all
select '1/8/2013','ABC','Test1',NULL union all
select '1/8/2013','XYZ','Test2',NULL union all
select '1/9/2013','ABC','Test1',NULL union all
select '1/9/2013','XYZ','Test2',NULL union all
select '1/10/2013','ABC','Test1',NULL union all
select '1/10/2013','XYZ','Test2',NULL union all
select '1/11/2013','ABC','Test1','-7.1' union all
select '1/11/2013','XYZ','Test2','-12.7'
;with stage as
(
select *, [r] = row_number() over (order by [Date])
from @Table
)
select l.[Date],
[OldValue] = l.value,
[NewValue] = isnull(l.Value, f.Value + (t.Value - f.Value) * (l.[r] - f.[r]) / (t.[r] - f.[r]))
from stage l
outer
apply ( select top 1 [Date], Value, [r]
from stage x
where x.[Date] < l.[Date] and
x.Value is not null
order
by [Date] desc
) f
outer
apply ( select top 1 [Date], Value, [r]
from stage x
where x.[Date] > l.[Date] and
x.Value is not null
order
by [Date] asc
) t
order
by [date] asc;