Re: SQL Question - Mailing list pgsql-general

From Bruno Wolff III
Subject Re: SQL Question
Date
Msg-id 20050415230526.GA12716@wolff.to
Whole thread Raw
In response to SQL Question  (Alex <alex@meerkatsoft.com>)
List pgsql-general
On Fri, Apr 15, 2005 at 21:58:31 +1000,
  Alex <alex@meerkatsoft.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;

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: Catching dangling LOBs?
Next
From: Vitaly Belman
Date:
Subject: Re: Catching dangling LOBs?