Re: SQL Question

From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: Alex <alex(at)meerkatsoft(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: SQL Question
Date: 2005-04-15 23:05:26
Message-ID: 20050415230526.GA12716@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, Apr 15, 2005 at 21:58:31 +1000,
Alex <alex(at)meerkatsoft(dot)com> wrote:
> Hi,
>
> i have a table
>
> ProdId | LastUpdate
> -------+------------
> 100 | 2005-04-01
> 100 | 2005-03-01
> 100 | 2005-02-01
> 200 | 2005-04-01
> 200 | 2005-03-01
> 200 | 2005-02-01
>
> - How can i select only the newest record for each ProdId ?
>
> 100 | 2005-04-01
> 200 | 2005-04-01

You can also use the Postgres specific DISTINCT ON clause.

SELECT DISTINCT ON (prodid) prodid, lastupdate ORDER BY prodid, lastupdate DESC;

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Vitaly Belman 2005-04-15 23:06:17 Re: Catching dangling LOBs?
Previous Message Tom Lane 2005-04-15 22:40:56 Re: Catching dangling LOBs?