oracle中decode的一些巧妙用法
2024-08-24 03:04:06
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
最新文章
- Android基础总结(四)
- HTML5- Canvas入门(二)
- Android 实战之UI线程和Worker线程交互
- codeforces 450 B Jzzhu and Sequences
- Linux 查看版本详情
- 硝烟中的scrum学习笔记 - 怎样制定Sprint计划(Plan Meeting)
- Shell/Bash 变量/variable 循环/loop
- VSS Admin 清除密码
- [Javascript] Logging Pretty-Printing Tabular Data to the Console
- 闲来瞎扯 -- 在vs2008下编写linux程序
- IOS中TableView的用法
- 【超级干货】手机移动端WEB资源整合:转载
- create groups 和 create folder reference
- Fedora25和win10双系统安装及使问题汇总
- 在配置github中遇到的一些问题
- Java 链接SQL Server 数据库
- laravel带参数分页
- MySQL Err(1024):Lock wait timeout exceeded; try restarting transaction
- [Linux]Debian 9重启DNS重置问题
- LuoGu P1352 没有上司的舞会
热门文章
- [翻译]成为顶尖程序员应当学什么?Python、C还是Ruby?
- 青出于蓝而胜于蓝 — Vue.js对Angular.js的那些进步
- Linux内核互斥锁--mutex
- SQLite的总结与在C#的使用
- spring +springmvc+mybatis组合web.xml文件配置
- 使用intelliJ创建 spring boot + gradle + mybatis站点
- CJOJ 1308 【HNOI 2002 】营业额统计 / CodeVS 1296 营业额统计(STL,二分)
- 关于net mail组件ssl端口问题
- webgl开发第一道坎——矩阵与坐标变换
- ES6正则表达式扩展