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

最新文章

  1. Android基础总结(四)
  2. HTML5- Canvas入门(二)
  3. Android 实战之UI线程和Worker线程交互
  4. codeforces 450 B Jzzhu and Sequences
  5. Linux 查看版本详情
  6. 硝烟中的scrum学习笔记 - 怎样制定Sprint计划(Plan Meeting)
  7. Shell/Bash 变量/variable 循环/loop
  8. VSS Admin 清除密码
  9. [Javascript] Logging Pretty-Printing Tabular Data to the Console
  10. 闲来瞎扯 -- 在vs2008下编写linux程序
  11. IOS中TableView的用法
  12. 【超级干货】手机移动端WEB资源整合:转载
  13. create groups 和 create folder reference
  14. Fedora25和win10双系统安装及使问题汇总
  15. 在配置github中遇到的一些问题
  16. Java 链接SQL Server 数据库
  17. laravel带参数分页
  18. MySQL Err(1024):Lock wait timeout exceeded; try restarting transaction
  19. [Linux]Debian 9重启DNS重置问题
  20. LuoGu P1352 没有上司的舞会

热门文章

  1. [翻译]成为顶尖程序员应当学什么?Python、C还是Ruby?
  2. 青出于蓝而胜于蓝 — Vue.js对Angular.js的那些进步
  3. Linux内核互斥锁--mutex
  4. SQLite的总结与在C#的使用
  5. spring +springmvc+mybatis组合web.xml文件配置
  6. 使用intelliJ创建 spring boot + gradle + mybatis站点
  7. CJOJ 1308 【HNOI 2002 】营业额统计 / CodeVS 1296 营业额统计(STL,二分)
  8. 关于net mail组件ssl端口问题
  9. webgl开发第一道坎——矩阵与坐标变换
  10. ES6正则表达式扩展