In principle I have 2 tables T1 and T2, each containing a date-field, lets call it date:
T1: date | somekey | data ... T2: date | somekey | data ...
(left)join T1 and T2, such that the result has all rows from T1.
Now for each row from T1 I need the values from the (exactly one) row of T2 joined where
ABS(DATEDIFF(T1.date,T2.date)) is minimal and
T1.somekey=T2.somekey (or null in case there is no such row in T2)
preguntado el 12 de junio de 12 a las 09:06
No lo hagas en el
JOIN clause, just put it in the
SELECT * FROM T1 LEFT JOIN T2 ON T1.somekey = T2.somekey WHERE ABS(DATEDIFF(T1.date,T2.date)) = 1 /*or whatever "minimal" means for you*/ /*or like this:*/ < $yourMinimalValue
I think I get what you want to do, but subqueries are the way to go here:
SELECT * FROM T1 LEFT JOIN T2 ON T1.somekey = T2.somekey WHERE ABS(DATEDIFF(T1.date,T2.date)) = (SELECT MIN(DATEDIFF(TT1.date, TT2.date)) FROM T1 AS TT1 LEFT JOIN T2 AS TT2 ON TT1.somekey = TT2.somekey)