ORACLE自定义顺序排序

ORACLE可以借助DECODE函数,自定义顺序排序:

select * from (
    select Nick as item from dual
    union all
    select Viki as item from dual
    union all
    select Glen as item from dual
    union all
    select Robin as item from dual
    union all
    select Total as item from dual
) pre_tab
order by decode(item, Viki, 1, Glen, 2, Robin, 3, Nick, 4, Total, 99);

 

另外,在Report开发中,常需要将Total放最后,其它项则按其它排序方式(一般按正常的升序),可看作同一列有两种排序方式,那么可以这样:

select * from (
    select Nick as item from dual
    union all
    select Viki as item from dual
    union all
    select Glen as item from dual
    union all
    select Robin as item from dual
    union all
    select Total as item from dual
) pre_tab
order by decode(item, Total, 2, 1), item;

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。