Reputation: 11820
I have the following data:
id date_modified value
1 some_date 4
1 some_date 3
1 some_date 1
2 some_date 5
3 some_date 8
3 some_date 7
...
and this query gets the id
and date_modified
where date_modified
is the latest date:
select bh.id, max(bh.date_modified)
from my_table bh
group by bh.id;
which does this:
id date_modified
1 some_date
2 some_date
3 some_date
...
where each date is the latest date for each id
which is nearly what I want.
How do I change the group by
to get the value
out? It's the value
that I'm interested in but I need to group using the id
.
Many thanks.
Upvotes: 1
Views: 95
Reputation: 33381
Try this:
SELECT id, date_modified, value
FROM
(
SELECT
id, date_modified, value, ROW_NUMBER() OVER (PARTITIOn BY Id ORDER BY date_modified DESC) RN
FROM tbl
) A
WHERE RN = 1
Upvotes: 1