Assume I have table exection_records
which has the data:
+----+----------+---------------------+
| id | handle | finishedAt |
+----+----------+---------------------+
| 1 | task_foo | 2015-08-16 03:10:33 |
| 2 | task_foo | 2015-08-15 04:00:27 |
| 3 | task_foo | 2015-08-14 02:10:25 |
| 4 | task_bar | 2015-08-17 03:00:25 |
| 5 | task_bar | 2015-08-16 02:01:25 |
| 6 | task_bar | 2015-08-13 06:02:50 |
+----+----------+---------------------+
Now I want to get the row where finishedAt
at is at its most recent timestamp for each unique handle, that is:
+----+----------+---------------------+
| id | handle | finishedAt |
+----+----------+---------------------+
| 1 | task_foo | 2015-08-16 03:01:33 |
| 4 | task_bar | 2015-08-17 03:00:25 |
+----+----------+---------------------+
I know that there is MAX
in MySQL.
I could get the very latest record for each task via:
SELECT *,MAX(finishedAt) FROM db.execution_records where taskHandle = 'task_foo';
SELECT *,MAX(finishedAt) FROM db.execution_records where taskHandle = 'task_bar';
Yet I do not want to issue multiple queries but one, and I do not want to name the handles.
How could I achieve my query?