Sql 需要从稀疏填充的表中插补缺失的数据

Sql 需要从稀疏填充的表中插补缺失的数据,sql,sql-server,Sql,Sql Server,我试图从一个现有的稀疏表中填充一个临时报告表,该表包含两个关键元素——基于日期范围的日期键和价格,以及价格变化时的价格。以下是价格变动表中的数据: 2天前滚动7天周期报告的日期范围包括6/12-6/19。表中的两行的旧价格和旧价格差异从6/7超出了该报告的范围,但是,需要该价格和差异才能将其添加到所有日期键20190612到20190618的现金价格列下。在6月19日,出现了新的价格变化/差异。在那上面 datekey,新的价格/差异值应更改 报告所需的设置数据如下: 以下是构建采样数据的代码:

我试图从一个现有的稀疏表中填充一个临时报告表,该表包含两个关键元素——基于日期范围的日期键和价格,以及价格变化时的价格。以下是价格变动表中的数据:

2天前滚动7天周期报告的日期范围包括6/12-6/19。表中的两行的旧价格和旧价格差异从6/7超出了该报告的范围,但是,需要该价格和差异才能将其添加到所有日期键20190612到20190618的现金价格列下。在6月19日,出现了新的价格变化/差异。在那上面 datekey,新的价格/差异值应更改

报告所需的设置数据如下:

以下是构建采样数据的代码:

-- T-SQL script to build the sampling tables

-- use for date-related cross-join later
if object_id( N'tempdb..#Numbers', N'U' ) is not null 
drop table #Numbers;

create table #Numbers(
   n int
 );

insert #Numbers( n ) values( 0 ), ( 1 ), ( 2 ), ( 3 ), ( 4 ), ( 5 ), ( 6 ), 
( 7 ), ( 8 ), ( 9 ), ( 10 ) 


-- select * from #Numbers;

-- creating existing sparse price data
if object_id( N'tempdb..#dt', N'U' ) is not null 
 drop table #dt;

create table #dt(
  StoreNumber int
  , City char( 3 )
  , State char( 2 )
  , Type char( 1 )
  , ProductKey int
  , DateKey int
  , CashPrice money
  , DateLastPriceChange datetime
  , CashPriceVar money
 )

 insert #dt values
    ( 1, 'OKC', 'OK', 'D', 144, 20190607, 2.799, '2019-06-07 11:37', -0.1 )
  , ( 1, 'OKC', 'OK', 'D', 144, 20190619, 2.699, '2019-06-19 10:40', -0.1 )

-- select * from #dt;

-- creaing temporary working table for reporting
if object_id( N'tempdb..#tt', N'U' ) is not null 
  drop table #tt;

create table #tt(
  StoreNumber int
, City char( 3 )
, State char( 2 )
, Type char( 1 )
, ProductKey int
, DateKey int
-- a couple of extra columns here
, DateKeyDate date
, DataDateKey int
, CashPrice money
, DateLastPriceChange datetime
, CashPriceVar money
)

-- dub in the start date for the report
declare @StartDateKey date = '2019-06-12'

-- populate the temporary working table
insert #tt( StoreNumber, ProductKey, DateKeyDate )
 select distinct
   dt.StoreNumber
 , dt.ProductKey
 , dateadd( day, n.n, @StartDateKey )
 from #dt dt
  cross join #Numbers n
 where
  n.n <= 7

-- select * from #tt

-- change the added datekeydate to datekey format
update #tt
set DateKey = year( DateKeyDate ) * 10000 + month( DateKeyDate ) * 100 + 
    day( DateKeyDate )
这是我一直在编写的代码,它步履蹒跚。这并不理想,在许多情况下,针对全套数据,它过滤掉了我交叉加入的日期,因此我没有输入的数据——只显示原始的已知价格变化。请告知

select 
dd.StoreNumber
, dto.City
, dto.State
, dto.Type
, dd.ProductKey
, dd.DateKey
, dto.CashPrice
, dto.DateLastPriceChange
, dto.CashPriceVar
from (
 select 
    tt.StoreNumber
    , tt.ProductKey
    , tt.DateKey
    , max( dt.DateKey ) MaxDateKey
 from #tt tt
  inner join #dt dt
    on dt.StoreNumber = tt.StoreNumber
    and dt.ProductKey = tt.ProductKey
    and dt.DateKey <= tt.DateKey
 group by
    tt.StoreNumber
    , tt.ProductKey
    , tt.DateKey
) dd
inner join #dt dto
    on dto.StoreNumber = dd.StoreNumber
    and dto.ProductKey = dd.ProductKey
    and dto.DateKey = dd.MaxDateKey;
编辑:是否有人可以建议SQL Server LEAD函数是否是一个更好的选择,或者将当前行的值与下一行的值进行比较