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;

复制代码

作者:Nick Huang 博客:http://www.cnblogs.com/nick-huang/ 本博客为学习、笔记之用,以笔记形式记录学习的知识与感悟。学习过程中可能参考各种资料,如觉文中表述过分引用,请务必告知,以便迅速处理。如有错漏,不吝赐教。

猜你喜欢

转载自blog.csdn.net/qq_26171035/article/details/83750641