oracle之按照指定順序排序輸出某些字段


摘自:

https://www.cnblogs.com/nick-huang/p/4076273.html

 

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;

附 一條: 添加字段,排序 可以這樣(比如下面的 年份 nf)

order by nf, decode(dq,'貴陽',1,'遵義',2,'六盤水',3,'安順',4,'都勻',5,'凱里',6,'銅仁',7,'興義',8,'畢節',8,'貴安',10,'黔江',11,'送周邊',12)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM