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 6C869D1EA97 for ; Thu, 22 Apr 2004 03:02:02 -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 87313-08 for ; Thu, 22 Apr 2004 03:01:46 -0300 (ADT) Received: from www.postgresql.com (www.postgresql.com [200.46.204.209]) by svr1.postgresql.org (Postfix) with ESMTP id 5D9B5D1E12A for ; Thu, 22 Apr 2004 03:01:33 -0300 (ADT) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by www.postgresql.com (Postfix) with ESMTP id 1CE41CF6735 for ; Thu, 22 Apr 2004 00:30:59 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.12.11/8.12.11) with ESMTP id i3M3UwPT018806; Wed, 21 Apr 2004 23:30:58 -0400 (EDT) To: Heflin Cc: Bruno Wolff III , pgsql-sql@postgresql.org Subject: Re: Join issue on a maximum value In-reply-to: <4086D95A.5000609@tampabay.rr.com> References: <4086BD8E.90900@tampabay.rr.com> <20040421185122.GA31195@wolff.to> <4086D95A.5000609@tampabay.rr.com> Comments: In-reply-to Heflin message dated "Wed, 21 Apr 2004 16:28:10 -0400" Date: Wed, 21 Apr 2004 23:30:58 -0400 Message-ID: <18805.1082604658@sss.pgh.pa.us> From: Tom Lane 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/219 X-Sequence-Number: 17594 Heflin writes: > Bruno Wolff III wrote: >> The postgres specific way of doing this is: >> SELECT DISTINCT ON (auction.auction_id) >> 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' >> ORDER BY auction.auction_id, image.image_id DESC > The thing that disturbs me about your syntax is that I don't really see > an assurance that I'll get the correct image_id. Any chance you can > tell me why this works? The ORDER BY DESC is what forces the max image_id to be selected. Read the discussion of SELECT DISTINCT ON in the SELECT reference page; the "weather report" example may be illuminating. regards, tom lane