Re: order by question

Lists: pgsql-admin
From: Tony Liao <tonyliao(at)yuehetone(dot)com>
To: pgsql-admin(at)postgresql(dot)org
Subject: order by question
Date: 2009-06-19 09:50:25
Message-ID: 8f750b7c0906190250p854581en365005b49019912d@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-admin

Hi,All
for example,I have a query as this
select id,product_id from table_name where product_id in
(6,3,4,10,7) order by .....
the results I want to get as bellow:
id................product_id......................
33...............6...................................
40...............6..................................
12...............3....................................
25...............4.................................
10...............4................................
17...............10.................................
43................7...................................

any Idea? thanks

--
Tony Liao


From: Tino Schwarze <postgresql(at)tisc(dot)de>
To: pgsql-admin(at)postgresql(dot)org
Subject: Re: order by question
Date: 2009-06-19 12:10:13
Message-ID: 20090619121013.GC8809@easy2.in-chemnitz.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-admin

On Fri, Jun 19, 2009 at 05:50:25PM +0800, Tony Liao wrote:
> Hi,All
> for example,I have a query as this
> select id,product_id from table_name where product_id in
> (6,3,4,10,7) order by .....
> the results I want to get as bellow:
> id................product_id......................
> 33...............6...................................
> 40...............6..................................
> 12...............3....................................
> 25...............4.................................
> 10...............4................................
> 17...............10.................................
> 43................7...................................

What is the exact condition, you want to order by? There's no "question"
anywhere... What is the structure of your table?

Tino.

PS: The mailing lists pgsql-novice or pgsql-general might me more
suitable for such questions.

--
"What we nourish flourishes." - "Was wir nähren erblüht."

www.lichtkreis-chemnitz.de
www.craniosacralzentrum.de


From: Sergio Gabriel Rodriguez <sgrodriguez(at)gmail(dot)com>
To: Tony Liao <tonyliao(at)yuehetone(dot)com>
Cc: pgsql-admin(at)postgresql(dot)org
Subject: Re: order by question
Date: 2009-06-19 14:26:45
Message-ID: 15aa6b3e0906190726k51ebc64el34a12d4fd20b11e1@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-admin

On Fri, Jun 19, 2009 at 6:50 AM, Tony Liao <tonyliao(at)yuehetone(dot)com> wrote:
Hi,All
for example,I have a query as this
select id,product_id from table_name where product_id in
(6,3,4,10,7) order by .....
the results I want to get as bellow:
id................product_id......................
33...............6...................................
40...............6..................................
12...............3....................................
25...............4.................................
10...............4................................
17...............10.................................
43................7...................................

any Idea? thanks

--
Tony Liao

I suppose you want order by IN condition "(6,3,4,10,7)" (?)

Sergio Gabriel Rodriguez
http://www.3trex.com.ar


From: Kenny W Drobnack <kenny(dot)w(dot)drobnack(at)jpmchase(dot)com>
To: Tony Liao <tonyliao(at)yuehetone(dot)com>, "pgsql-admin(at)postgresql(dot)org" <pgsql-admin(at)postgresql(dot)org>
Subject: Re: order by question
Date: 2009-06-19 14:39:55
Message-ID: 6E25A151103CC340A1CB2CD7D44DCA4509BD84CAB4@EMASC218VS01.exchad.jpmchase.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-admin

I've never used "ORDER BY ....." in any of my queries. If you want to get the records in order by id, you would simply do "ORDER BY id". If you want them by product it would be "ORDER BY product_id".
Your best bet (depending on what you want) is likely "ORDER BY product_id, id" which will give you the records in order by product id and then if you have multiple records with the same product_id, it will also order by id. So your results would look like:

id product_id

12...............3....................................
10...............4................................
25...............4.................................
33...............6...................................
40...............6..................................
43...............7...................................
17...............10.................................

From: pgsql-admin-owner(at)postgresql(dot)org [mailto:pgsql-admin-owner(at)postgresql(dot)org] On Behalf Of Tony Liao
Sent: Friday, June 19, 2009 5:50 AM
To: pgsql-admin(at)postgresql(dot)org
Subject: [ADMIN] order by question

Hi,All
for example,I have a query as this
select id,product_id from table_name where product_id in (6,3,4,10,7) order by .....
the results I want to get as bellow:
id................product_id......................
33...............6...................................
40...............6..................................
12...............3....................................
25...............4.................................
10...............4................................
17...............10.................................
43................7...................................

any Idea? thanks

--
Tony Liao

This communication is for informational purposes only. It is not
intended as an offer or solicitation for the purchase or sale of
any financial instrument or as an official confirmation of any
transaction. All market prices, data and other information are not
warranted as to completeness or accuracy and are subject to change
without notice. Any comments or statements made herein do not
necessarily reflect those of JPMorgan Chase & Co., its subsidiaries
and affiliates.

This transmission may contain information that is privileged,
confidential, legally privileged, and/or exempt from disclosure
under applicable law. If you are not the intended recipient, you
are hereby notified that any disclosure, copying, distribution, or
use of the information contained herein (including any reliance
thereon) is STRICTLY PROHIBITED. Although this transmission and any
attachments are believed to be free of any virus or other defect
that might affect any computer system into which it is received and
opened, it is the responsibility of the recipient to ensure that it
is virus free and no responsibility is accepted by JPMorgan Chase &
Co., its subsidiaries and affiliates, as applicable, for any loss
or damage arising in any way from its use. If you received this
transmission in error, please immediately contact the sender and
destroy the material in its entirety, whether in electronic or hard
copy format. Thank you.

Please refer to http://www.jpmorgan.com/pages/disclosures for
disclosures relating to European legal entities.


From: Scott Marlowe <scott(dot)marlowe(at)gmail(dot)com>
To: Tony Liao <tonyliao(at)yuehetone(dot)com>
Cc: pgsql-admin(at)postgresql(dot)org
Subject: Re: order by question
Date: 2009-06-19 14:49:19
Message-ID: dcc563d10906190749j31cd561en474bbe3b7096268e@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-admin

On Fri, Jun 19, 2009 at 3:50 AM, Tony Liao<tonyliao(at)yuehetone(dot)com> wrote:
> Hi,All
>        for example,I have a query as this
>          select id,product_id from table_name where product_id in
> (6,3,4,10,7) order by .....
>        the results I want to get as bellow:
> id................product_id......................
> 33...............6...................................
> 40...............6..................................
> 12...............3....................................
> 25...............4.................................
> 10...............4................................
> 17...............10.................................
> 43................7...................................
>           any Idea? thanks

I take it you want the set sorted by product_id order of 6,3,4,10 ???

order by
case when product_id = 6 then 1 when product_id=3 then 2 when
product_id=4 then 3 when product_id=10 then 4 else product_id+1000
end;


From: Tony Liao <tonyliao(at)yuehetone(dot)com>
To: pgsql-admin(at)postgresql(dot)org
Subject: Re: order by question
Date: 2009-06-20 01:32:36
Message-ID: 8f750b7c0906191832y42889854kc66a618262eb2d27@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-admin

Thanks,All, To Tino,I want to order by IN condition "(6,3,4,10,7)".
To Sergio,my answer is "Yes".
To Kenny,I think this is not the result I want to get.
To Scott,In database,there are thousands of products.the select
query is generated by application,and it is not sure how many product_id
in condition,maybe the next query would be .........product_id in
(4,26,7,8,9,23,27,54)
Maybe,temp table is a solution,but that would be performance issue.

thanks

2009/6/19 Scott Marlowe <scott(dot)marlowe(at)gmail(dot)com>

> On Fri, Jun 19, 2009 at 3:50 AM, Tony Liao<tonyliao(at)yuehetone(dot)com> wrote:
> > Hi,All
> > for example,I have a query as this
> > select id,product_id from table_name where product_id in
> > (6,3,4,10,7) order by .....
> > the results I want to get as bellow:
> > id................product_id......................
> > 33...............6...................................
> > 40...............6..................................
> > 12...............3....................................
> > 25...............4.................................
> > 10...............4................................
> > 17...............10.................................
> > 43................7...................................
> > any Idea? thanks
>
> I take it you want the set sorted by product_id order of 6,3,4,10 ???
>
> order by
> case when product_id = 6 then 1 when product_id=3 then 2 when
> product_id=4 then 3 when product_id=10 then 4 else product_id+1000
> end;
>

--
Tony Liao


From: Scott Marlowe <scott(dot)marlowe(at)gmail(dot)com>
To: Tony Liao <tonyliao(at)yuehetone(dot)com>
Cc: pgsql-admin(at)postgresql(dot)org
Subject: Re: order by question
Date: 2009-06-20 01:46:46
Message-ID: dcc563d10906191846y300dbd9cla7e965f9b478c562@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-admin

On Fri, Jun 19, 2009 at 7:32 PM, Tony Liao<tonyliao(at)yuehetone(dot)com> wrote:
> Thanks,All,
>          To Tino,I want to order by IN condition "(6,3,4,10,7)".
>          To Sergio,my answer is "Yes".
>          To Kenny,I think this is not the result I want to get.
>          To Scott,In database,there are thousands of  products.the select
> query is generated by application,and it is not sure how many product_id
> in condition,maybe the next query would be .........product_id in
> (4,26,7,8,9,23,27,54)

If you generate the product_id in (4,26,7,8,9,23,27,54) part then
it's pretty easy to create the order by case part too.


From: Sergio Gabriel Rodriguez <sgrodriguez(at)gmail(dot)com>
To: Tony Liao <tonyliao(at)yuehetone(dot)com>
Cc: pgsql-admin(at)postgresql(dot)org
Subject: Re: order by question
Date: 2009-07-05 20:02:37
Message-ID: 15aa6b3e0907051302q6d848ee7k5345ace120b0b29b@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-admin

On Fri, Jun 19, 2009 at 10:46 PM, Scott Marlowe <scott(dot)marlowe(at)gmail(dot)com>wrote:

> On Fri, Jun 19, 2009 at 7:32 PM, Tony Liao<tonyliao(at)yuehetone(dot)com> wrote:
> > Thanks,All,
> > To Tino,I want to order by IN condition "(6,3,4,10,7)".
> > To Sergio,my answer is "Yes".
> > To Kenny,I think this is not the result I want to get.
> > To Scott,In database,there are thousands of products.the select
> > query is generated by application,and it is not sure how many product_id
> > in condition,maybe the next query would be .........product_id in
> > (4,26,7,8,9,23,27,54)
>
> If you generate the product_id in (4,26,7,8,9,23,27,54) part then
> it's pretty easy to create the order by case part too.
>
> I agree with Scott, if your application generate IN condition, could create
CASE too, looks like this

SELECT id, product_id FROM your_table
WHERE product_id IN (6, 3, 4, 10, 7)
ORDER BY
CASE
WHEN product_id = 6 THEN 1
WHEN product_id = 3 THEN 2
WHEN product_id = 4 THEN 3
WHEN product_id = 10 THEN 4
WHEN product_id = 7 THEN 5
END;

Regards,

Sergio Gabriel Rodriguez
http://www.3trex.com.ar


From: Harald Fuchs <hari(dot)fuchs(at)gmail(dot)com>
To: pgsql-admin(at)postgresql(dot)org
Subject: Re: order by question
Date: 2009-07-06 09:51:12
Message-ID: puhbxqywa7.fsf@srv.protecting.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-admin


> I agree with Scott, if your application generate IN condition, could create
> CASE too, looks like this

> SELECT id, product_id FROM your_table
>    WHERE product_id IN (6, 3, 4, 10, 7)
>     ORDER BY
>       CASE
>         WHEN product_id = 6  THEN 1
>         WHEN product_id = 3  THEN 2
>         WHEN product_id = 4  THEN 3
>         WHEN product_id = 10 THEN 4
>         WHEN product_id = 7  THEN 5
>       END;

A shorter solution would be

ORDER BY product_id != 6,
product_id != 3,
product_id != 4,
product_id != 10,
product_id != 7,
product_id