Skip to content

Groupwise Maximum

MySQL Groupwise Maximum#

A common problem, it seems is getting the most recent field/record along with every parent or related record in a dataset.

Most Recent Record#

So let us break it down and do it just for the table containing the maximum’s we want:

SELECT
  user_id, MAX(created_at)
FROM
  orders
GROUP BY
  user_id

I tried this, but unfortunately I got an error from mysql:

Lost connection to MySQL during query

Sources#