How to fetch data by only one of the two fields with different values in group by grouping in SQL

Table:
ID name value time
1 A 3 2018-1-1
1 A 4 2018-4-4
2 B 6 2018-1-2
3 C 7 2018-1-3
3 C 9 2018-4-6

how to group by ID and only take the most recent value of time (time) under the same ID and name:

such as:

ID name value time
1 A 4 2018-4-4
2 B 6 2018-1-2
3 C 9 2018-4-6

Mar.04,2021

if pg does not support partitioning functions, associate tables more than once, for example:

select id, name, time, max(value)
from t inner join (
select id, name, max(time) as max_time
from t
group by id, name
) t1 on t.id = t1.id and t.name = t1.name and t.time=t1.max_time
group by id, name, time

select * from (
select id,name, value,ROW_NUMBER () OVER (PARTITION BY a.id name ORDER BY a.time DESC) as num
from table a
) b
where b.num = 1

Menu