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

前端 未结 9 862
感动是毒
感动是毒 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:27

    There is a new option in Postgressql 9.5 called DISTINCT ON

    SELECT DISTINCT ON (location) location, time, report
        FROM weather_reports
        ORDER BY location, time DESC;
    

    It eliminates duplicate rows an leaves only the first row as defined my the ORDER BY clause.

    see the official documentation

提交回复
热议问题