0

I have a table like this:

id |       date | score
-----------------------
 1 | 2017-12-01 |    10
 1 | 2017-12-02 |    20
 2 | 2017-12-01 |    30
 2 | 2017-12-02 |    40
 2 | 2017-12-03 |    50
 3 | 2017-12-03 |    60

I want to get the latest score per id like so:

id |       date | score
-----------------------
 1 | 2017-12-02 |    20
 2 | 2017-12-03 |    50
 3 | 2017-12-03 |    60

So far I have something like this:

SELECT
    id,
    MAX(date)
FROM
    mytable
GROUP BY
    id

But I don't know how to get the score of the row of the MAX(date) per id

user3685285
  • 5,396
  • 10
  • 45
  • 89

1 Answers1

0
select t1.*
from mytable t1
join
(
    SELECT id, MAX(date) as mdate
    FROM mytable
    GROUP BY id
) t2 on t1.id = t2.id
    and t1.date = t2.mdate
juergen d
  • 195,137
  • 36
  • 275
  • 343