X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 54BA63A44D6 for ; Tue, 26 Oct 2004 22:58:12 +0100 (BST) 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 59955-08 for ; Tue, 26 Oct 2004 21:58:01 +0000 (GMT) Received: from anchor-post-34.mail.demon.net (anchor-post-34.mail.demon.net [194.217.242.92]) by svr1.postgresql.org (Postfix) with ESMTP id ADBB93A44D5 for ; Tue, 26 Oct 2004 22:58:03 +0100 (BST) Received: from lfix.demon.co.uk ([80.177.205.209] helo=cerberus.lfix.co.uk) by anchor-post-34.mail.demon.net with esmtp (Exim 4.42) id 1CMZKM-0004Jn-EC; Tue, 26 Oct 2004 21:58:02 +0000 Received: from linda.lfix.co.uk ([192.168.1.1]) by cerberus.lfix.co.uk with esmtp (Exim 4.34) id 1CMZKE-0005zL-6w; Tue, 26 Oct 2004 22:57:54 +0100 Subject: Re: How to re-sort a sorted query? From: Oliver Elphick Reply-To: olly@lfix.co.uk To: Tom Lane Cc: Yudie , pgsql-sql@postgresql.org In-Reply-To: <13739.1098827021@sss.pgh.pa.us> References: <003001c4bb91$4c6647c0$a101a8c0@home.axiontech.com> <1098822881.2414.42.camel@linda> <13739.1098827021@sss.pgh.pa.us> Content-Type: text/plain Organization: LFIX Limited Date: Tue, 26 Oct 2004 22:57:53 +0100 Message-Id: <1098827873.2414.47.camel@linda> Mime-Version: 1.0 X-Mailer: Evolution 2.0.2 Content-Transfer-Encoding: 7bit X-LFIX-MailScanner: Found to be clean X-MailScanner-From: olly@lfix.co.uk X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200410/193 X-Sequence-Number: 19296 On Tue, 2004-10-26 at 17:43 -0400, Tom Lane wrote: > Oliver Elphick writes: > > On Tue, 2004-10-26 at 14:23 -0500, Yudie wrote: > >> Then I do this query to get unique store number and also the cheapest > >> price from each store: > >> > >> SQL= "Select distinct on (storenumber), itemsku, storenumber,price > >> from storeproduct where itemsku='10001' > >> order by storenumber, price" > > > That won't get you the cheapest price, > > Sure it will. It's a perfectly good application of DISTINCT ON. > However, he has to use that particular ORDER BY to get the answers > he wants. Ah - because ORDER BY is applied before DISTINCT ON; I hadn't realised that. -- Oliver Elphick olly@lfix.co.uk Isle of Wight http://www.lfix.co.uk/oliver GPG: 1024D/A54310EA 92C8 39E7 280E 3631 3F0E 1EC0 5664 7A2F A543 10EA ======================================== "Whosoever therefore shall be ashamed of me and of my words in this adulterous and sinful generation; of him also shall the Son of man be ashamed, when he cometh in the glory of his Father with the holy angels." Mark 8:38