SQL - How to join on similar (not exact) columns

Posted by BlueRaja on Stack Overflow See other posts from Stack Overflow or by BlueRaja
Published on 2010-05-11T20:18:15Z Indexed on 2010/05/11 20:24 UTC
Read the original article Hit count: 294

Filed under:
|
|
|

I have two tables which get updated at almost the exact same time - I need to join on the datetime column.

I've tried this:

SELECT *
FROM A, B
WHERE ABS(DATEDIFF(second, A.Date_Time, B.Date_Time) = (
    SELECT MIN(ABS(DATEDIFF(second, A.Date_Time, B2.Date_Time)))
    FROM B AS B2
)

But it tells me:

Multiple columns are specified in an aggregated expression containing an outer reference. If an expression being aggregated contains an outer reference, then that outer reference must be the only column referenced in the expression.

How can I join these tables?

© Stack Overflow or respective owner

Related posts about sql

Related posts about tsql