I am having problems finding a solution for what I thought would be straight forward. Here is an example of what I am trying to do.
TABLE A
time | state
10:00 | up
10:09 | down
10:12 | up
TABLE B
time | data
10:05 | abc
10:07 | def
10:11 | ghi
I would like to join tables A and B maintaining all data in B, something like
SELECT tableB.time, tableB.data, tableA.status
INTO my_results
FROM tableB
LEFT JOIN tableA
WHERE tableB.time > (MAX(tableA.time) < tableB.time)
So the my_results table would look like the following:
TABLE my_results
time | data | state
10:05 | abc | up
10:07 | def | up
10:11 | ghi | down
Table A Structure
source, varchar
destination, varchar
time, datetime
status, varchar
Table B Structure
tstamp, datetime
source, varchar
destination, varchar
This is the relevant information. Hopefully this is helpful.
Here’s a solution for SQL Server:
Live example at SQL Fiddle.