PostgreSQL - fetch the row which has the Max value for a column

前端 未结 9 818
感动是毒
感动是毒 2020-11-28 18:38

I\'m dealing with a Postgres table (called \"lives\") that contains records with columns for time_stamp, usr_id, transaction_id, and lives_remaining. I need a query that wil

9条回答
  •  有刺的猬
    2020-11-28 19:32

    Actaully there's a hacky solution for this problem. Let's say you want to select the biggest tree of each forest in a region.

    SELECT (array_agg(tree.id ORDER BY tree_size.size)))[1]
    FROM tree JOIN forest ON (tree.forest = forest.id)
    GROUP BY forest.id
    

    When you group trees by forests there will be an unsorted list of trees and you need to find the biggest one. First thing you should do is to sort the rows by their sizes and select the first one of your list. It may seems inefficient but if you have millions of rows it will be quite faster than the solutions that includes JOIN's and WHERE conditions.

    BTW, note that ORDER_BY for array_agg is introduced in Postgresql 9.0

提交回复
热议问题