Re: How to re-sort a sorted query? - Mailing list pgsql-sql

From Tom Lane
Subject Re: How to re-sort a sorted query?
Date
Msg-id 13739.1098827021@sss.pgh.pa.us
Whole thread Raw
In response to Re: How to re-sort a sorted query?  (Oliver Elphick <olly@lfix.co.uk>)
Responses Re: How to re-sort a sorted query?
List pgsql-sql
Oliver Elphick <olly@lfix.co.uk> 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.

So the only way (I think) to change the ordering for display is to
wrap this as a sub-select:

select * from (select distinct on (storenumber), itemsku, storenumber,price  from storeproduct where itemsku='10001'
orderby storenumber, price) ss
 
order by price;
        regards, tom lane


pgsql-sql by date:

Previous
From: "Yudie"
Date:
Subject: Re: How to re-sort a sorted query?
Next
From: Oliver Elphick
Date:
Subject: Re: How to re-sort a sorted query?