我正在选择不同范围内的记录,例如
select t.range as profit_range, count(*) as number_ of_occurences from (
select case
when t.range between 100000000 and 200000000 then '100 million - 200 million'
when t.range between 200000000 and 300000000 then '200 million - 300 million'
.
.
.
when t.range between 1000000000 and 2000000000 then '1000 million - 2000 million'
else 'Greater then 2000 milions' end as range
from tbl_profit) t
group by profit_range
这样可行,但结果行的顺序不正确
Profit_Range No_of_occurences
100 million - 200 million 4
1000 million - 2000 million 1
200 million - 300 million 4
如何订购结果以正确的顺序显示?
答案 0 :(得分:1)
select t.range as [profit range], count(*) as [number of occurences]
from (
select case
when profit between 100000000 and 200000000 then '100 million - 200 million'
when profit between 200000000 and 300000000 then '200 million - 300 million'
.
.
.
when profit between 1000000000 and 2000000000 then '1000 million - 2000 million'
else 'Greater then 2000 milions' end as range
from tbl_profit) t
group by t.range
Order by SUBSTR(t.range,1,LOCATE(‘million’,t.range)-1)
您可以使用SUBSTR()
和LOCATE
函数从范围中提取起始编号,并根据提取的编号对结果进行排序