Get the latest date from grouped MySQL data Get the latest date from grouped MySQL data mysql mysql

Get the latest date from grouped MySQL data


Are you looking for the max date for each model?

SELECT model, max(date) FROM docGROUP BY model

If you're looking for all models matching the max date of the entire table...

SELECT model, date FROM docWHERE date IN (SELECT max(date) FROM doc)

[--- Added ---]

For those who want to display details from every record matching the latest date within each model group (not summary data, as asked for in the OP):

SELECT d.model, d.date, d.color, d.etc FROM doc dWHERE d.date IN (SELECT max(d2.date) FROM doc d2 WHERE d2.model=d.model)

MySQL 8.0 and newer supports the OVER clause, producing the same results a bit faster for larger data sets.

SELECT model, date, color, etc FROM (SELECT model, date, color, etc,   max(date) OVER (PARTITION BY model) max_date FROM doc) predoc WHERE date=max_date;


You can try using max() in subquery, something like this :

SELECT model, date  FROM doc WHERE date in (SELECT MAX(date) from doc GROUP BY model);


Subquery giving dates. We are not linking with the model. So below query solves the problem.

If there are duplicate dates/model can be avoided by the following query.

select t.model, t.datefrom doc tinner join (select model, max(date) as MaxDate from doc  group by model)tm on t.model = tm.model and t.date = tm.MaxDate