D
D
di2019-12-06 12:04:32
PostgreSQL
di, 2019-12-06 12:04:32

How to select latest entries for each raw_id?

There is a table with records of this kind
5dea165bb575c933032888.png
How can I elegantly get the last single_sum for each raw_id in one query. so
5dea16bb20a25366774222.png
far, nothing better than this has come up

select
    id,
    raw_id,
    single_sum
from
    my_table
where
    id in (
        select
            max(id)
  from
            my_table
        group by
            raw_id)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Melkij, 2019-12-06
@Delgus

select distinct on (raw_id) raw_id, id, single_sum 
from tablename
order by raw_id, id desc

Maybe I mixed up the sort direction as usual.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question