So I have 2 tables. In first table I need to link ID with min and max datetime, which I did with
SELECT id, min (datetime) as earlytime, max(datetime) as latesttime
FROM table1
group by id
as a result I get around 5k with ID and 2 timestamps.
I need to use this info to get rows with ID from table2 where datetime for each ID respectively is between their earlytime and latesttime. Or just rows with ID and time which less than latesttime.
Still can't figure out how to do that.
Came up with something like that
SELECT ID, source, amount, type, datetime
FROM table2
WHERE EXISTS (SELECT ID, min (datetime) as earlytime, max (datetime) as latetime
FROM table1
group by ID)
But I guess it just shows any rows that match ID from table1
Anyone can help me with that?