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 69B9B3A44CC for ; Tue, 26 Oct 2004 22:34:21 +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 52488-04 for ; Tue, 26 Oct 2004 21:34:10 +0000 (GMT) Received: from axiontech.com (unknown [216.52.172.78]) by svr1.postgresql.org (Postfix) with SMTP id 177153A44BB for ; Tue, 26 Oct 2004 22:34:11 +0100 (BST) Received: (qmail 814 invoked from network); 26 Oct 2004 21:34:23 -0000 Received: from pc-00161 (HELO yudie) (192.168.1.161) by mail.axiontech.com (192.168.1.6) with SMTP; 26 Oct 2004 21:34:23 -0000 Message-ID: <004d01c4bba3$5d992f00$a101a8c0@home.axiontech.com> From: "Yudie" To: References: <003001c4bb91$4c6647c0$a101a8c0@home.axiontech.com> <1098822881.2414.42.camel@linda> Subject: Re: How to re-sort a sorted query? Date: Tue, 26 Oct 2004 16:32:57 -0500 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2800.1106 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 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/191 X-Sequence-Number: 19294 Oliver ,sorry, I didn't submit all complete fields as my example, The reason I didn't use agregate function because I still need to select another fields from storeproduct table and some outer joins. What about if the data in storeproduct table shows like this: ItemSku , StoreNumber , Price, Condition ========================== 10001 , 7 , 30.00, Used 10001 , 7 , 35.00, New <-- duplicate store number 10001 , 5 , 45.00, New 10001 , 2 , 50.00, New However, should I use temporary table to make it simple? what about the performance? Yudie ----- Original Message ----- From: "Oliver Elphick" To: "Yudie" Cc: Sent: Tuesday, October 26, 2004 3:34 PM Subject: Re: [SQL] How to re-sort a sorted query? On Tue, 2004-10-26 at 14:23 -0500, Yudie wrote: > I have a query that need to be sorted in order of price of store's > product with unique store number. > > Here is a sample data of storeproduct table: > > > ItemSku , StoreNumber , Price > ========================== > 10001 , 7 , 30.00 > 10001 , 7 , 35.00 <-- duplicate store number > 10001 , 5 , 45.00 > 10001 , 2 , 50.00 > > 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, just an arbitrary one determined by the physical storage order. You need to use GROUP BY with an aggregate function: SELECT itemsku, storenumber, MIN(price) FROM storeproduct WHERE itemsku = '10001' GROUP BY itemsku, storenumber ORDER BY price, storenumber; > Result #1: > ItemSku , StoreNumber , Price > 10001 , 2 , 50.00 > 10001 , 5 , 45.00 > 10001 , 7 , 30.00 > > The question is how to make the query that returns as above but sorted > by price? The literal answer to your question is to put price first in the ORDER BY clause, but I'm not convinced you actually want to know something that simple. -- 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 ---------------------------(end of broadcast)--------------------------- TIP 8: explain analyze is your friend