ORDER BY handling mixed integer and varchar values - Mailing list pgsql-sql

From David B
Subject ORDER BY handling mixed integer and varchar values
Date
Msg-id c76ea21c050516114742818cfa@mail.gmail.com
Whole thread Raw
Responses Re: ORDER BY handling mixed integer and varchar values  (PFC <lists@boutiquenumerique.com>)
Re: ORDER BY handling mixed integer and varchar values  (Edmund Bacon <ebacon@onesystem.com>)
Re: ORDER BY handling mixed integer and varchar values  (Rod Taylor <pg@rbt.ca>)
Re: ORDER BY handling mixed integer and varchar values  (Ragnar Hafstað <gnari@simnet.is>)
List pgsql-sql
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.

pgsql-sql by date:

Previous
From: PFC
Date:
Subject: Re: choosing index to use
Next
From: PFC
Date:
Subject: Re: ORDER BY handling mixed integer and varchar values