I have a list of components which periodically report status.
I want to write a query to find a list of the most recent status grouped by component.
Typically I would use the solution described by the accepted answer for this question: MySQL order by before group by
However there can be multiple statuses reported each second, so there is no guarantee that I will retrieve the most recent. Therefore I would like to find the status with the most recent timestamp and in the case of duplicate timestamps, the one with the highest PK.
Ideally I would like to have a query like the following:
SELECT *
FROM component_status
ORDER BY component_status.timestamp DESC, component_status.component_status_id DESC
GROUP BY component_status.component_id;
However you cannot perform a GROUP BY after the ORDER BY.
Has anybody had a similar problem and found a solution?