oracle中decode()函数

本文介绍了一种使用DECODE函数改进SQL查询结果排序的方法。通过具体案例展示了如何实现区级领导批示事件优先显示,随后是区级职能部门上报事件,最后是普通事件的排序需求。
[size=medium][color=red][b]decode(待比较的对象,如果为这个,那么结果为这个,否则结果是这个) [/b][/color][/size]

表issueNew.xml中
字段commandState值:
1代表区级别的领导批示,0代表其他级别的领导的批示,null代表未批示。


字段orgtypeInternalid的值:
1代表区级职能部门上报的事件,0代表其他部门的事件。

[color=blue]
需求 :查出的信息这样排序:先区级领导批示的事件,后区级职能部门上报的事件,最后是普通的事件。[/color]

我写的sql:
select iu.subject, iu.commandState,orgtypeInternalid from issuesnew iu,issuelogsnew il
where il.targeorginternalcode like '1.1.1.1.'||'%' and iu.id=il.issueid and iu.id not in(select issueid from historicalIssues
where orgId=4 or orgInternalCode like '1.1.1.1.'||'%')
order by iu.commandState desc,orgtypeInternalid desc


查询结果如下:

[img]http://dl.iteye.com/upload/attachment/0070/9604/b4b7e201-b614-39e1-bc54-ea072a52303b.jpeg[/img]

[b][color=red][size=large]显然不正确。正确写法如下:[/size][/color][/b]


select iu.subject, iu.commandState,from issuesnew iu,issuelogsnew il
where il.targeorginternalcode like '1.1.1.1.'||'%' and iu.id=il.issueid and iu.id not in(select issueid from historicalIssues
where orgId=4 or orgInternalCode like '1.1.1.1.'||'%')
order by [color=red][b]decode(iu.commandState,null,0,iu.commandState) desc[/b][/color],orgtypeInternalid desc


查询结果如下:


[img]http://dl.iteye.com/upload/attachment/0070/9612/1a7b42b3-2c35-37b2-8eae-f35eb855c45d.jpeg[/img]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值