order by 子句里含有decode函数的说明

The following query shows the current wait state of each slave (child process) and query coordinator process on the system:

SELECT px.SID "SID", p.PID, p.SPID "SPID", px.INST_ID "Inst",
       px.SERVER_GROUP "Group", px.SERVER_SET "Set",
       px.DEGREE "Degree", px.REQ_DEGREE "Req Degree", w.event "Wait Event"
FROM GV$SESSION s, GV$PX_SESSION px, GV$PROCESS p, GV$SESSION_WAIT w
WHERE s.sid (+) = px.sid AND s.inst_id (+) = px.inst_id AND
      s.sid = w.sid (+) AND s.inst_id = w.inst_id (+) AND
      s.paddr = p.addr (+) AND s.inst_id = p.inst_id (+)
ORDER BY DECODE(px.QCINST_ID,  NULL, px.INST_ID,  px.QCINST_ID), px.QCSID, 
DECODE(px.SERVER_GROUP, NULL, 0, px.SERVER_GROUP), px.SERVER_SET, px.INST_ID;

参考:http://docs.oracle.com/cd/E11882_01/server.112/e25523/parallel006.htm#i1008510


语句说明:

order by 子句里含有decode函数时,decode函数里的第一个参数,是order by排序时要针对的字段(对象,对哪些字段进行排序操作),而decode函数则是影响order by排序时数据行的顺序。例如,

表A的内容如下:

Aa          Ab

--------    ----------

1            19

null        2

4            10

3             1

SQL>select * from A order by Aa;

Aa          Ab

--------    ----------

1            19

3             1

4            10

null        2

SQL>select * from A  ORDER BY DECODE(Aa, NULL,Ab);

Aa          Ab

--------    ----------

1            19

null       2

3             1

4           10


上述两个例子里order by排序针对的字段都是Aa,而带有decode函数的语句排序时发现某行的里Aa字段为null时就会用该行的Ab字段的值赋予 Aa字段,故而Aa字段以此值来参加排序。


decode用法详见:

Oracle DECODE函数的语法介绍以及使用技巧




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值