I have a pretty complex query (lots of joins) that returns this dataset:
+------------+-----------+-----------+---------------------+---------------------+-----------+------------+
| vehicle_id | pickup_id | driver_id | pickup_time | dropoff_time | code | name |
+------------+-----------+-----------+---------------------+---------------------+-----------+------------+
| 13 | 154 | 23 | 2021-08-18 22:30:00 | 2021-08-18 23:30:00 | 17526 | Natalie |
| 13 | 16 | 23 | 2021-08-18 23:35:00 | 2021-08-19 00:35:00 | 17245 | Mark |
| 12 | 2879 | 15 | 2021-08-19 12:00:00 | 2021-08-19 21:00:00 | 17517 | Will |
+------------+-----------+-----------+---------------------+---------------------+-----------+------------+
If I add a GROUP BY vehicle_id
to this query I get the Mark and Will rows, but I'd expect the Natalie row. Even if I change the inner query
Here is the full query, with the inner simplified a little bit:
SELECT * FROM (
SELECT *
FROM trips
WHERE vehicle_id IS NOT NULL
ORDER BY pickup_time ASC
) AS tmp
GROUP BY vehicle_id
Regardless if I do the ORDER BY
with ASC
or DESC
I'm still getting the Mark and Will records. I cannot get the Natalie one.
What am I doing wrong with my query?