Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Data Bases > Pgsql Novice > Re: ORDER BY Cl...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 3 Topic 3071 of 3191
Post > Topic >>

Re: ORDER BY Clause

by fbax@[EMAIL PROTECTED] (Frank Bax) Apr 13, 2008 at 12:06 AM

Derrick Betts wrote:
> I have a query: SELECT fruit, group_number, ordering_number FROM 
> fruit_groups ORDER BY group_number, order_number;
> 
> The results look like:
>    fruit   |   group_number   |   ordering_number
> --------+------------------+-------------------
> cherry  |              1             |              1
> orange |              1             |              2
>   apple |              2             |              1
>     pear |              2             |              2
> banana |              3             |              1
>     kiwi |               3            |              2
> 
> 
> I would like the results to look like:
>    fruit   |   group_number   |   ordering_number
> --------+------------------+-------------------
>   apple |              2             |              1
> orange |              2             |              2
> banana |              3             |              1
>     kiwi |               3            |              2
> cherry  |              1             |              1
> orange |              1             |               2
> 
> I want the group_number to be grouped together, then sorted by the 
> ordering_number, then listed in the output alphabetically by the fruit 
> name listed first in the group_number (as defined by the
ordering_number).


What??  In your sample data the fruit that comes "first" alphabetically 
within each group also happens to have ordering_number = 1; so which 
field do you want to sort on?

You will want either:

select fruit_groups.* from fruit_groups
left join
(select group_number,min(ordering_number) as order
from fruit_groups group by group_number) as sort
on sort.group_number=fruit_groups.group_number
order by sort.order, group_number, ordering_number;

or

select fruit_groups.* from fruit_groups
left join
(select group_number,min(fruit) as order
from fruit_groups group by group_number) as sort
on sort.group_number=fruit_groups.group_number
order by sort.order, group_number, ordering_number;

Only min() changed.

-- 
Sent via pgsql-novice mailing list (pgsql-novice@[EMAIL PROTECTED]
)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-novice
 




 3 Posts in Topic:
ORDER BY Clause
list@[EMAIL PROTECTED] (  2008-04-12 20:52:38 
Re: ORDER BY Clause
fbax@[EMAIL PROTECTED] (  2008-04-13 00:06:01 
Re: ORDER BY Clause
list@[EMAIL PROTECTED] (  2008-04-13 12:38:58 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Aug 21 18:17:05 CDT 2008.