X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id C2399D1DCD0 for ; Wed, 21 Apr 2004 16:19:31 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 99753-01 for ; Wed, 21 Apr 2004 16:19:29 -0300 (ADT) Received: from denny.farviolet.com (dsl092-017-235.sfo4.dsl.speakeasy.net [66.92.17.235]) by svr1.postgresql.org (Postfix) with ESMTP id 954A0D1DB8B for ; Wed, 21 Apr 2004 16:19:26 -0300 (ADT) Received: from denny.farviolet.com (localhost [127.0.0.1]) by denny.farviolet.com (8.12.10/8.12.10/Debian-4) with ESMTP id i3LJCmKu019329; Wed, 21 Apr 2004 12:12:48 -0700 Received: (from jrs@localhost) by denny.farviolet.com (8.12.10/8.12.10/Debian-4) id i3LJCmHU019327; Wed, 21 Apr 2004 12:12:48 -0700 From: Jeremy Semeiks Date: Wed, 21 Apr 2004 12:12:48 -0700 To: Heflin , pgsql-sql@postgresql.org Subject: Re: Join issue on a maximum value Message-ID: <20040421191248.GA18969@64.81.242.180> References: <4086BD8E.90900@tampabay.rr.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4086BD8E.90900@tampabay.rr.com> User-Agent: Mutt/1.5.4i X-Virus-Scanned: by amavisd-new at postgresql.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200404/286 X-Sequence-Number: 17661 On Wed, Apr 21, 2004 at 02:29:34PM -0400, Heflin wrote: > OK, it's been a while since I've had to do anything remotely complex in > SQL, so this may just be a pure brain block on my part. > ... > > So a basic JOIN gets this: > > SELECT auction.auction_id, image.image_id, image.image_descr > FROM auction JOIN image ON auction.auction_id = image.auction_id > WHERE auction.auction_owner = 'Mabel'; > > auction_id | image_id | image_descr > ------------+----------+------------- > 1 | 1 | image 1 > 1 | 2 | image 2 > 2 | 3 | image 3 > 3 | 4 | image 4 > 3 | 5 | image 5 > 3 | 7 | image 8 > (6 rows) > > Now the problem: I can't seem to remember how to get only the max value > for the image_id for each auction_id so that the result set would be: > > auction_id | image_id | image_descr > ------------+----------+------------- > 1 | 2 | image 2 > 2 | 3 | image 3 > 3 | 7 | image 8 > > Playing with the max() aggregate seems to be the correct path, but for > the life of me I can't seem to get the syntax to the point that it > produces what I need. Any help would be greatly appreciated! A simple way to write this is to use DISTINCT ON combined with ORDER BY. In this case, these clauses can substitute for the MAX aggregate: select distinct on (auction.auction_id) auction.auction_id, image.image_id, image.image_descr from auction join image using (auction_id) where auction.auction_owner = 'Mabel' order by auction.auction_id, image.image_id desc; (I haven't tested this.) I think you need a subselect in there if you want to use the MAX aggregate. - Jeremy