Re: Three fields table: id-data-date_time, how to get max() and date_time same time? - Mailing list pgsql-general

From Sam Mason
Subject Re: Three fields table: id-data-date_time, how to get max() and date_time same time?
Date
Msg-id 20091106111433.GW5407@samason.me.uk
Whole thread Raw
In response to Re: Three fields table: id-data-date_time, how to get max() and date_time same time?  (Chris <dmagick@gmail.com>)
List pgsql-general
On Fri, Nov 06, 2009 at 02:09:03PM +1100, Chris wrote:
> select max(data), id, (select max(date_time) from test where id=t1.id)
> from test t1 group by id;

I'd tend to use the DISTINCT ON[1] operator for these sorts of problems:

  SELECT DISTINCT ON (id) *
  FROM test
  ORDER BY id, data DESC;

--
  Sam  http://samason.me.uk/

 [1] http://www.postgresql.org/docs/current/static/sql-select.html#SQL-DISTINCT

pgsql-general by date:

Previous
From: John R Pierce
Date:
Subject: Re: xml import/export tools and performance
Next
From: Arjen Nienhuis
Date:
Subject: Re: MD5 Authentication