我真的不知道如何解决这个问题,需要一些帮助——不仅是解决方案,而且是如何解决的想法都会受到欢迎。
我有下表:
TABLE Data
( 
     RecordID
    ,DateAdd
    ,Status
)
示例日期如下:
11  2012-10-01 OK
11  2012-10-04 NO
11  2012-11-05 NO
22  2012-10-01 OK
33  2012-11-01 NO
33  2012-11-15 OK
此表具有以下示例数据:
TABLE Periods
(
    PeriodID
   ,PeriodName
   ,DateStart
   ,DateEnd
)
1 Octomer  2012-10-01 2012-10-31
2 November 2012-11-01 2012-11-30
我需要做的是填充一个新表:
TABLE DataPerPeriods
(
    PeriodID,
    RecordID,
    Status
)
这将存储 PeriodID 和 RecordID 的所有可能组合以及 period 的最新状态(如果可用)。如果给定期间的状态不可用,则为以前期间的状态。如果根本没有以前的状态 - 那么状态为 NULL。
例如,对于以下数据,我需要这样的东西:
1 11 NO //We have status "OK" and "NO", but "NO" is latest for the period
1 22 OK 
1 33 NULL//Because there are no records for this and previous periods
2 11 NO //We get the previos status as there are no records in this periods
2 22 OK //There are not records for this period, but record for last periods is available
2 33 NO //We have status "OK" and "NO", but "OK" is latest for the period
编辑:我已经在最后一个表中填充了期间 id 和记录 id,我需要更多关于状态更新的帮助。