左側のテーブルの日付値が右側のテーブルのタイムスロットに収まる2つのテーブル間の結合を作成しようとしています。したがって、たとえば、私が持っている場合:
TABLE A TABLE B
ID TIMESTMP ID TIMESMTP VALUE
1 8/31/2012 2:00 PM 1 8/30/2012 4:00 AM A
2 8/29/2012 3:00 PM 1 8/31/2012 1:00 PM B
3 7/04/2012 5:00 AM 1 8/31/2012 3:00 PM C
2 8/20/2012 1:00 PM A
結果は次のようになります。
TABLE C
ID TIMESTMP VALUE
1 8/31/2012 2:00 PM B
2 8/29/2012 3:00 PM A
3 7/04/2012 5:00 AM null
テーブルBで、最大タイムスタンプがまだ<テーブルAのタイムスタンプである対応するレコードを検索したい。一致するID(外部結合)がない場合、またはBにタイムスタンプがない場合<Aのタイムスタンプである必要があります。 nullを返します。
ありがとう!
アップデート
Gordon Linoffによって提案されたlead()を使用して行った解決策は次のとおりです。
SELECT b.value, a.*
FROM table_a a
LEFT OUTER JOIN (
SELECT id, timestmp,
lead(timestmp) over(PARTITION BY id ORDER BY timestmp) AS next_timestmp,
value FROM table_b
) b
ON a.id = b.id
AND (a.timestmp >= b.timestmp AND (a.timestmp < b.timestmp OR b.timestmp IS NULL))