Thread: order by question
--
Tony Liao
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
On Fri, Jun 19, 2009 at 6:50 AM, Tony Liao <tonyliao@yuehetone.com> wrote:
--
Tony Liao
I suppose you want order by IN condition "(6,3,4,10,7)" (?)
Sergio Gabriel Rodriguez
http://www.3trex.com.ar
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@postgresql.org [mailto:pgsql-admin-owner@postgresql.org] On Behalf Of Tony Liao
Sent: Friday, June 19, 2009 5:50 AM
To: pgsql-admin@postgresql.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.
On Fri, Jun 19, 2009 at 3:50 AM, Tony Liao<tonyliao@yuehetone.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;
On Fri, Jun 19, 2009 at 3:50 AM, Tony Liao<tonyliao@yuehetone.com> wrote:I take it you want the set sorted by product_id order of 6,3,4,10 ???> 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
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
On Fri, Jun 19, 2009 at 7:32 PM, Tony Liao<tonyliao@yuehetone.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.
On Fri, Jun 19, 2009 at 7:32 PM, Tony Liao<tonyliao@yuehetone.com> wrote:If you generate the product_id in (4,26,7,8,9,23,27,54) part then
> 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)
it's pretty easy to create the order by case part too.
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
> 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