我真的沒有想法如何解決這個問題,需要一些幫助 - 不僅解決方案,但如何接近的想法將受到歡迎。T-SQL需要複雜連接幫助
我有如下表:
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的所有可能組合,以及期限的最新狀態(如果可用)。如果狀態在給定期間不可用,則爲以前期間的狀態。如果根本沒有以前的狀態 - 則表示狀態爲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在最後一個表,我需要狀態更新更多的幫助。