1.oracle中decode的一些巧妙用法
2017-07-18 10:22
716 查看
1、符号函数sign在decode中的用法--比较大小
select decode(sign(变量1-变量2),-1,变量1,变量2) from dual; --取较小值
sign()函数根据某个值是0、正数还是负数,分别返回0、1、-1
例如:
变量1=10,变量2=20
则sign(变量1-变量2)返回-1,decode解码结果为“变量1”,达到了取较小值的目的。
2.比如我要查询某班男生和女生的数量分别是多少?
通常我们这么写:
select count(*) from 表 where 性别 = 男;
select count(*) from 表 where 性别 = 女;
要想显示到一起还要union一下,太麻烦了
用decode呢,只需要一句话:
select count(decode(xb,男性,1,null)),count(decode(xb,女性,1,null)) from Table
注:count在统计总数时,null所在的列不被计算在内
本文的参考链接:http://www.cnblogs.com/allanzhang/p/6388966.html
相关文章推荐
- [Oracle] decode 函数及其用法
- Oracle 中 decode 函数用法
- IT忍者神龟之Oracle 中 decode 函数用法
- Oracle 中 decode 函数用法
- oracle聚合函数rank()的用法和一些体会
- Oracle 中 decode 函数用法
- [Oracle] decode 函数及其用法
- Oracle 中 decode 函数用法
- Oracle 中 decode 函数用法(转)
- dual的一些巧妙用法
- oracle 中一些独特的函数用法
- case 和DECODE在oracle里用法
- oracle中 alter 用法 与 decode 用法
- Oracle decode的用法
- oracle decode,grouping,group by rollup的用法实例
- Oracle 中 decode 函数用法
- 数据库ORACLE中函数decode的用法
- [Oracle] decode 函数及其用法
- Oracle中case when和decode的用法
- Oracle 中 decode 函数用法