Sql 在oracle查询中按价格范围排序
我有以下行的价格范围,$0-$50,$50-$100,$100-$500,$500+定义为varchar2列。我想按照上面给定的顺序对具有值的行进行排序。谁能给我一个建议吗。你可以使用regexp\u substr做你想做的事。但是,情况可能最简单:Sql 在oracle查询中按价格范围排序,sql,oracle,Sql,Oracle,我有以下行的价格范围,$0-$50,$50-$100,$100-$500,$500+定义为varchar2列。我想按照上面给定的顺序对具有值的行进行排序。谁能给我一个建议吗。你可以使用regexp\u substr做你想做的事。但是,情况可能最简单: order by (case pricerange when '$0-$50' then 1 when '$50-$100' then 2 when '$100-$500' t
order by (case pricerange
when '$0-$50' then 1
when '$50-$100' then 2
when '$100-$500' then 3
when '$500+' then 4
else 999
end)
也许你考虑过这样的事情。 宽度\u BUCKETexp、最小值、最大值、铲斗\u cnt。函数将exp分配给它的bucket。它创建了一个U cnt+2 下面的示例创建了5个Bucket WIDTH_BUCKETprice 50500,3
你能提供你已经尝试过的吗?我不知道从哪里开始以及如何…请提供表格结构、样本数据和期望的结果。猜测您希望使用case语句按顺序排列。如果我使用case,如何执行desc顺序?只需在表达式:end desc之后添加desc。我可以像这样在order by中使用decode和regexp_substr吗?按decodesignregexp_substr'1 asc nulls last','^\d*'-7',-1','1 asc nulls last',case pricerange当'$0-$50',然后1当'$50-$100',然后2当'$100-$500',然后3当'$500+',然后4否则999结束,因为它对我不起作用,此查询的排序失败。您可能可以使用decode和regexp\u substr。我发现这个解决方案更复杂。而且,我更喜欢大小写而不是解码。你能给我一个查询示例吗?
(500-50)/3 = 150
0 - <50 less than min
1 - (50 : 200)
2 - (200 : 350)
3 - (350 : 500)
4 - 500> more than max
with prod_table as( select 'A' prod_name, 49 price from dual
union all
select 'B' prod_name, 100 price from dual
union all
select 'c' prod_name, 200 price from dual
union all
select 'd' prod_name, 300 price from dual
union all
select 'e' prod_name, 1000 price from dual)
SELECT prod_name, price,WIDTH_BUCKET(price,50,500,3) from prod_table order by WIDTH_BUCKET(price,50,500,3) desc