M
M
moem2017-07-14 12:59:52
SQL
moem, 2017-07-14 12:59:52

How to select the rows with the maximum values ​​of one of the fields?

Hello.
Given: view table

objectdatetimeparameter
one2013one
22015one
32015one
420153
22016one
320172

Please tell me how to select rows for distinct object where datetime will be maximum. Those. the result should look like this
objectdatetimeparameter
one2013one
420153
22016one
320172

Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2017-07-14
@moem

SELECT `t`.*
  FROM (
    SELECT `object`, MAX(`datetime`) AS `maxtime`
    FROM `table`
    GROUP BY `object`
  ) AS `t1`
  JOIN `table` AS `t` ON `t`.`object` = `t1`.`object` 
    AND `t`.`datetime` = `t1`.`maxtime`

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question