pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Bruno Wolff III <bruno@wolff.to>
To: Alex <alex@meerkatsoft.com>
Cc: pgsql-general@postgresql.org
Subject: Re: SQL Question
Date: Fri, 15 Apr 2005 18:05:26 -0500
Message-ID: <20050415230526.GA12716@wolff.to> (raw)
In-Reply-To: <425FAC67.2050203@meerkatsoft.com>
References: <425FAC67.2050203@meerkatsoft.com>

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;



view thread (71+ messages)  latest in thread

Message-ID: <20050415230526.GA12716@wolff.to>
Permalink:  ../20050415230526.GA12716@wolff.to/
Also on:    postgresql.org/message-id/20050415230526.GA12716@wolff.to

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: bruno@wolff.to, alex@meerkatsoft.com
  Subject: Re: SQL Question
  In-Reply-To: <20050415230526.GA12716@wolff.to>
  References: <425FAC67.2050203@meerkatsoft.com> <20050415230526.GA12716@wolff.to>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox