Oracle 根据列值排序

潜力骨 / 2023-05-08 / 原文

利用decode函数
order by DECODE(hist.SERVICE_TYPE,'SEX','1','SIM','2','AEX','3','AIM','4','ALL','5');
利用case when子句
select * from 表 order by (select case sunxun when 'A' then 3 when 'B' then 1 when 'C' then 2 when 'D' then 4 end);
ORDER BY 中关于NULL的处理
缺省处理,Oracle在Order by 时认为null是最大值,所以如果是ASC升序则排在最后,DESC降序则排在最前。
当然,你也可以使用nulls first 或者nulls last 语法来控制NULL的位置。
Nulls first和nulls last是Oracle Order by支持的语法
如果Order by 中指定了表达式Nulls first则表示null值的记录将排在最前(不管是asc 还是 desc)
如果Order by 中指定了表达式Nulls last则表示null值的记录将排在最后 (不管是asc 还是 desc)
使用语法如下:
将nulls始终放在最前
select * from zl_cbqc order by cb_ld nulls first
将nulls始终放在最后
select * from zl_cbqc order by cb_ld desc nulls last
几种排序的写法
单列升序:select from order by ; (默认升序,即使不写ASC)
单列降序:select from order by desc;
多列升序:select , from order by , ;
多列降序:select , from order by desc, desc;
多列混合排序:select , from order by desc, asc;