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;
复制代码

 

posted @   nick_huang  阅读(30462)  评论(1编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示