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

From: Sam Mason <sam(at)samason(dot)me(dot)uk>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Three fields table: id-data-date_time, how to get max() and date_time same time?
Date: 2009-11-06 11:14:33
Message-ID: 20091106111433.GW5407@samason.me.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Arjen Nienhuis 2009-11-06 11:24:27 Re: MD5 Authentication
Previous Message John R Pierce 2009-11-06 09:28:33 Re: xml import/export tools and performance