Reputation: 1323
I want to get the result set in the descending range order. Below is my Query
select quantity_range as quantity_range, count(*) as number_of_items,
sum(amount) as total_amount,
from (
select *,case
when quantity between 0 and 500 then '<=500'
when quantity between 501 and 525 then '501-525'
when quantity between 526 and 550 then '526-550'
when quantity between 551 and 575 then '551-575'
when quantity between 576 and 600 then '576-600'
when quantity between 601 and 625 then '601-625'
when quantity between 626 and 650 then '626-650'
when quantity between 651 and 675 then '651-675'
when quantity between 676 and 700 then '676-700'
when quantity between 701 and 725 then '701-725'
when quantity between 726 and 750 then '726-750'
else '>750' end as quantity_range
from Sales )
group by quantity_range order by quantity_range;
I want my Result set like:
<=500 100 100000.00
600-625 10 5000.00
>700 25 25000.00
How to get this ordering? If i give Order By clause then >700
coming at 2nd position.
Upvotes: 1
Views: 79
Reputation: 176264
Use RIGHT
to get last number from string:
select quantity_range as quantity_range, count(*) as number_of_items,
sum(amount) as total_amount
from (
select *,case
when quantity between 0 and 500 then '<=500'
when quantity between 501 and 525 then '501-525'
when quantity between 526 and 550 then '526-550'
when quantity between 551 and 575 then '551-575'
when quantity between 576 and 600 then '576-600'
when quantity between 601 and 625 then '601-625'
when quantity between 626 and 650 then '626-650'
when quantity between 651 and 675 then '651-675'
when quantity between 676 and 700 then '676-700'
when quantity between 701 and 725 then '701-725'
when quantity between 726 and 750 then '726-750'
else '>750' end as quantity_range
from Sales ) as sub
group by quantity_range
order by RIGHT(quantity_range,3);
Upvotes: 4