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 494B852913 for ; Mon, 16 May 2005 16:18:37 -0300 (ADT) 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 03722-07 for ; Mon, 16 May 2005 19:18:28 +0000 (GMT) Received: from happy.onesystem.ca (happy.onesystem.ca [205.206.61.17]) by svr1.postgresql.org (Postfix) with ESMTP id 29BB052921 for ; Mon, 16 May 2005 16:18:22 -0300 (ADT) Received: from elb-lx.onesystem.ca ([10.1.1.50]) by happy.onesystem.ca with asmtp (Exim 4.14) id 1DXl6c-00015X-MV; Mon, 16 May 2005 13:18:22 -0600 Message-ID: <4288F1FE.5050002@onesystem.com> Date: Mon, 16 May 2005 13:18:22 -0600 From: Edmund Bacon User-Agent: Mozilla Thunderbird 0.9 (X11/20041103) X-Accept-Language: en-us, en MIME-Version: 1.0 To: pgsql-sql@postgresql.org Cc: David B Subject: Re: ORDER BY handling mixed integer and varchar values References: In-Reply-To: Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests= X-Spam-Level: X-Archive-Number: 200505/113 X-Sequence-Number: 21426 Well the following seems to work, althoug I do not believe it is guarenteed to: (select * from table where column ~'\\d+' order by cast(colum as integer)) union all (select * from table where column !~ '\\d+' order by column); This could be quite slow if table is large Alternatively: select * from table order by case when column ~ '\\d+' cast(column as integer) else null end, column) This will sort all 'integer' values of column ahead of non-integer values. If you want non-integer then integer use ... else -1 end, ... (assuming all integer values of column are >= 0) David B wrote: >Hi All, >I have a tabe > >Product_desc varchar(100) >Product_price integer >Product_cat varchar(100) > >The problem… > >We have categories such as: > >Electronics >White Goods >1 >2 >5 >15 >25 >etc > >I have a query > >Select product_desc, product_price, product_cat >Order by product_cat, product_price > >And of course I get stuff ordered as I want it. >BUT… with many product categories being numeric based they come out in >wrong order '10 comes before 2" etc. > >So I tried >Select product_desc, product_price, product_cat >Order by cast(product_cat as integer), product_price > >And that worked for the numberic based categories. > >I don't know of a query will be across alpha or numeric categories. > >Is there any elegent query you folks can think of that combines the >two so I can one query that has alpha sorting on alpha categories and >numeric sorting on numeric values that are in the same column?? > >Tia. > >---------------------------(end of broadcast)--------------------------- >TIP 8: explain analyze is your friend > > -- Edmund Bacon