I have two tables that look like this:
Communication: (Calls made)
Timestamp FromIDNumber ToIDNumber GeneralLocation
2012-03-02 09:02:30 878 674 Grasslands
2012-03-02 11:30:01 456 213 Tundra
2012-03-02 07:02:12 789 654 Mountains
Movement:
Timestamp IDNumber Type X Y
2012-03-02 11:02:30 379 pedestrian 32 46
2012-03-01 12:32:41 654 pedestrian 54 56
2012-03-02 07:02:16 789 pedestrian 39 52
And the following query:
SELECT c.FromIDNumber, m.Timestamp, m.X, m.Y
FROM Communication c
JOIN Movement m ON c.FromIDNumber = m.IDNumber
WHERE m.TimeStamp = (SELECT MIN(mm.Timestamp)
FROM Movement mm
WHERE mm.TimeStamp >= c.TimeStamp)
Basically, it goes through every row of Commmunication
and finds the location of a call made by finding the closest movement timestamp for a given communication timestamp. (Here's the SQL Fiddle).
The problem for me is that it only returns the closest movement timestamp
that is after a certain communication timestamp
. For example, given this simple example:
Communication Time:
1:10 pm
Movement Data:
1:09 pm (100,100)
1:15 pm (200,200)
The query will return the 1:15pm (200,200)
, even though the closest timestamp
is the 1:09 pm. How would I write an sql query to find the closest time? I found a kinda similar SO post for sql-server
, but how would I do this in MySQL
?
I just don't have much experience in SQL, so help would be greatly appreciated thanks!!!