Diagnose_Date に最も近い運動日を抽出したい 2 つのテーブル Diagnose & Exercise があります。
where条件でDATEDIFF関数を使用して左結合を試みました
SELECT D.ID,D.Diagnose_Date,D.Type1,D.Type2,E.Exercise_Date],E.Field1,E.Field2,E.Field3
FROM Diagnose D
LEFT JOIN Exercise E
ON D.ID=E.ID
WHERE DATEDIFF(DAY,[Diagnose_Date],[Exercise_Date]) BETWEEN -30 AND 30
どんな助けもとても役に立ちます
前もって感謝します
診断表
------------------------------------------
ID Dignose_Date Type1 SubType1
------------------------------------------
1 10/01/2010 01 1.1
2 20/02/2012 02 2.2
3 30/03/2013 01 1.2
------------------------------------------
エクササイズテーブル
------------------------------------------
ID Exercise_Date Field1 Field2 Field3
------------------------------------------
1 01/01/2010 x y z
2 10/02/2012 a b c
2 01/04/2012 e f f
3 01/03/2013 x y z
3 05/04/2013 a b c
3 01/06/2013 x y z
------------------------------------------
期待される結果は次のとおりです。
------------------------------------------------------------------------
ID Diagnose_Date Exercise_Date Type1 SubType2 Field1 Field2 Field3
------------------------------------------------------------------------
1 10/01/2010 01/01/2010 01 1.1 x y z
2 20/02/2012 10/02/2012 02 2.2 a b c
3 30/03/2013 05/04/2013 01 1.2 a b c
-------------------------------------------------------------------------