X-Original-To: pgsql-general-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 21ABA53A2F for ; Fri, 15 Apr 2005 19:46:14 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 48268-07 for ; Fri, 15 Apr 2005 22:46:04 +0000 (GMT) Received: from wolff.to (wolff.to [66.93.249.74]) by svr1.postgresql.org (Postfix) with SMTP id 0704653800 for ; Fri, 15 Apr 2005 19:46:03 -0300 (ADT) Received: (qmail 13069 invoked by uid 500); 15 Apr 2005 23:05:26 -0000 Date: Fri, 15 Apr 2005 18:05:26 -0500 From: Bruno Wolff III To: Alex Cc: pgsql-general@postgresql.org Subject: Re: SQL Question Message-ID: <20050415230526.GA12716@wolff.to> Mail-Followup-To: Bruno Wolff III , Alex , pgsql-general@postgresql.org References: <425FAC67.2050203@meerkatsoft.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <425FAC67.2050203@meerkatsoft.com> User-Agent: Mutt/1.5.6i X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.009 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200504/680 X-Sequence-Number: 76593 On Fri, Apr 15, 2005 at 21:58:31 +1000, Alex 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;