Proper way to access latest row for each individual identifier?

后端 未结 5 579
轻奢々
轻奢々 2021-01-03 11:14

I have a table core_message in Postgres, with millions of rows that looks like this (simplified):

┌────────────────┬──         


        
5条回答
  •  遥遥无期
    2021-01-03 11:56

    In Postgres, I recommend distinct on:

    SELECT DISTINCT ON (mmsi) m.*
    FROM core_message m
    ORDER BY mmsi, time DESC;
    

    For best performance, you want an index on (mmsi, time desc).

提交回复
热议问题