I am having a hard time coming up with SQL statement for this situation:
Imagine there are multiple vehicles on the road at any given time,
each of these trips is in the trips table.
Each vehicle regularly sends out the updates of its current location (lat, lon)
There are 2 tables:
current_position
id | trip_id | update_time | lat | lon
trips
id | status
current_position has multiple updates for each trip
I need to get results, having
trip_id, lat, lon
for each trip that has status = ‘DRIVING’
and must have the latest values from
current_position
Latest value I mean highest value of update_time.
When I tried the GROUP BY I got distinct results but the results are were not always the latest position. I can’t seem to combine the order by update_time DESC with GROUP BY trip_id
it sounds like you want to select the current_position row with the largest update_time value for a given trip_id. You can do this with a subquery using MAX.
Edit…after posting, i realize you need a join to get all rows where the corresponding trip status = ‘driving’. I think this will do it (although I am more Sql Server so there might be some slight difference with MySQL I’m unaware of).