一、常用函数详解

【a】nvl(a,b)函数: 如果a的值为空,那么取b的值

with temp1 as(select '张三' as name, '10' as text from dual),
temp2 as(select '' as name, '20' as text from dual),
temp3 as(select '李四' as name, '' as text from dual)--nvl(a,b)函数: 如果a的值为空,那么取b的值
select nvl(name, '无名氏') as name, nvl(text, '未知') as textfrom temp1
union all
select nvl(name, '无名氏') as name, nvl(text, '未知') as textfrom temp2
union all
select nvl(name, '无名氏') as name, nvl(text, '未知') as text from temp3

【b】nvl2(a,b,c)函数: 如果a的值不为空,那么取b的值,如果a的值为空,那么取c的值(类似三目运算符)

with temp1 as(select '张三' as name, '10' as text from dual),
temp2 as(select '' as name, '20' as text from dual),
temp3 as(select '李四' as name, '' as text from dual)--nvl2(a,b,c)函数: 如果a的值不为空,那么取b的值,如果a的值为空,那么取c的值(类似三目运算符)
select nvl2(name, name, '无名氏') as name, nvl2(text, text, '未知') as textfrom temp1
union all
select nvl2(name, name, '无名氏') as name, nvl2(text, text, '未知') as textfrom temp2
union all
select nvl2(name, name, '无名氏') as name, nvl2(text, text, '未知') as textfrom temp3

【c】sign(number) :  如果number大于0,sign则返回1;如果number小于0,sign则返回-1;如果number等于0,sign则返回0.

--sign(number)    如果number大于0,sign则返回1;如果number小于0,sign则返回-1;如果number等于0,sign则返回0
select sign(20 - 10), sign(10 - 20), sign(20 - 20), sign(20.0001 - 20.0000)from dual;

【d】substr(str,a,b): 字符串截取函数

--str :待截取字符串
--a :截取开始位置
--b :截取个数

注意:当a为负数时,只要 |a| ≤ b,取a的个数(如:7、8、9);当 |a| ≥ b时,才取b的个数

--字符截取函数substr(str,a,b)
--str 待截取字符串
--a 截取开始位置
--b 截取个数--注意:当a为负数时,只要 |a| ≤ b,取a的个数(如:7、8、9);当 |a| ≥ b时,才取b的个数select substr('zhangsan', 0, 3),  --zhasubstr('zhangsan', 1, 3),  --zhasubstr('zhangsan', 3, 5),  --angsasubstr('zhangsan', -1, 3), --nsubstr('zhangsan', -3, 2)  --safrom dual;

【e】listagg() ..within group() : 合并数据

with temp1 as(select '10001' as categoryId, '苹果' as categoryName from dual),
temp2 as(select '10001' as categoryId, '雪梨' as categoryName from dual),
temp3 as(select '10002' as categoryId, '铅笔' as categoryName from dual),
temp4 as(select '10003' as categoryId, '水果刀' as categoryName from dual),
temp5 as(select '10002' as categoryId, '钢笔' as categoryName from dual),
allResult as(select *from temp1union allselect *from temp2union allselect *from temp3union allselect *from temp4union allselect *from temp5)select * from allResult;

假如我们需要将categoryId相同的categoryName合并在一起显示,那么可以使用listagg within group函数来实现。

with temp1 as(select '10001' as categoryId, '苹果' as categoryName from dual),
temp2 as(select '10001' as categoryId, '雪梨' as categoryName from dual),
temp3 as(select '10002' as categoryId, '铅笔' as categoryName from dual),
temp4 as(select '10003' as categoryId, '水果刀' as categoryName from dual),
temp5 as(select '10002' as categoryId, '钢笔' as categoryName from dual),
allResult as(select *from temp1union allselect *from temp2union allselect *from temp3union allselect *from temp4union allselect *from temp5)select t.categoryId,listagg(to_char(t.categoryName), ',') within group(order by t.categoryName) as categoryNamefrom allResult tgroup by t.categoryId

【f】类型转换相关函数: to_char()/to_date()/to_number()

--to_char() 将查询结果转换为字符类型

--to_number() 将字符串类型转换为数字类型
--select to_number('zhangsan') from dual;  --无效数字

--to_date() 将字符型数据转换为日期型数据

--to_char() 将查询结果转换为字符类型--to_number() 将字符串类型转换为数字类型
--select to_number('zhangsan') from dual;  --无效数字--to_date() 将字符型数据转换为日期型数据select to_char(123456) as str,to_char(sysdate, 'YYYY-MM-DD HH24:MI:SS') as now,to_number('123456') as num,to_date('2018-11-09', 'YYYY-MM-DD') as dfrom dual;

【g】extract(fmt from d):提取日期中的特定部分,如年、月、日等

--fmt 为:year、month、day、hour、minute、second。其中 year、month、day可以为 data 类型匹配,也可以与 timestamp 类型匹配;但是 hour、minute、second 必须与 timestamp 类型匹配。
--hour 匹配的结果中没有加上时区,在中国运行的结果小 8 小时。

--extract(fmt from d),提取日期中的特定部分。
--fmt 为:year、month、day、hour、minute、second。其中 year、month、day可以为 data 类型匹配,也可以与 timestamp 类型匹配;但是 hour、minute、second 必须与 timestamp 类型匹配。
--hour 匹配的结果中没有加上时区,在中国运行的结果小 8 小时。select sysdate as now,extract(year from sysdate) as 年,extract(month from sysdate) as 月,extract(day from sysdate) as 天,extract(hour from systimestamp) as 小时_小8小时,extract(hour from systimestamp) + 8 as 小时,extract(minute from systimestamp) as 分钟,extract(second from systimestamp) as 秒from dual;

【h】decode(value,if1,then1,if2,then2,if3,then3,...,else) 

(1). 匹配是否相等(当然也可以使用case when 语句实现同样效果)

with temp1 as(select 'female' as sex from dual),
temp2 as(select 'male' as sex from dual),
temp3 as(select '' as sex from dual),
res as(select sexfrom temp1union allselect sexfrom temp2union allselect sexfrom temp3)--select sex from res;    --decode(value,if1,then1,if2,then2,if3,then3,...,else) --用法一:匹配是否相等(当然也可以使用case when 语句实现同样效果)--可以这样理解:
--如果 sex='female' 返回  '女'
--如果 sex='male' 返回  '男'
--否则,返回'未知'    select sex as 性别标识,decode(sex, 'female', '女', 'male', '男', '未知') as 性别from res;

(2). 结合sign用于比较大小
--select decode(sign(变量1-变量2),-1,变量1,变量2) from dual; --取较小值
--sign()函数根据某个值是0、正数还是负数,分别返回0、1、-1

--用法二:结合sign用于比较大小
--select decode(sign(变量1-变量2),-1,变量1,变量2) from dual; --取较小值
--sign()函数根据某个值是0、正数还是负数,分别返回0、1、-1--取较小值/较大值
select decode(sign(99 - 88), -1, 99, 88) as min, decode(sign(99 - 88), 1, 99, 88) as max from dual;

(3). 示例:成绩>85,显示优秀;>70显示良好;>60及格;否则是不及格。

--示例:成绩>85,显示优秀;>70显示良好;>60及格;否则是不及格。
with temp as(select '85' as scorefrom dualunion allselect '80' as scorefrom dualunion allselect '75' as scorefrom dualunion allselect '70' as scorefrom dualunion allselect '60' as scorefrom dualunion allselect '55' as scorefrom dual)--select score from temp;--实现方法一:
select score,decode(sign(score - 85),1,'优秀',0,'优秀',-1,decode(sign(score - 70),1,'良好',0,'良好',-1,decode(sign(score - 60), 1, '及格', 0, '及格', '不及格'))) as 成绩等级from temp;--实现方法二:
with temp as(select '85' as scorefrom dualunion allselect '80' as scorefrom dualunion allselect '75' as scorefrom dualunion allselect '70' as scorefrom dualunion allselect '60' as scorefrom dualunion allselect '55' as scorefrom dual)
select score, casewhen score >= 85 then'优秀'when score >= 70 then'良好'when score >= 60 then'及格'else'不及格'end as 成绩等级from temp;

【i】grouping(): 在分组统计时用到,grouping只能在使用rollup或cube的查询中使用 .

grouping(xxx) 返回0或者1。如果列值为空,那么grouping()返回1;如果列值非空,那么返回0。

with temp as(select '10001' as depid, '12000' as salary, 'zhangsan' as n, '01' as jidfrom dualunion allselect '10001' as depid, '10000' as salary, 'lisi' as n, '02' as jidfrom dualunion allselect '10002' as depid, '1000' as salary, 'wangwu' as n, '01' as jidfrom dualunion allselect '10002' as depid, '2000' as salary, 'zhaoliu' as n, '03' as jidfrom dualunion allselect '10003' as depid, '15000' as salary, 'tianqi' as n, '01' as jidfrom dualunion allselect '10003' as depid, '15000' as salary, 'wangba' as n, '02' as jidfrom dualunion allselect '10004' as depid, '15000' as salary, 'jiudan' as n, '01' as jidfrom dual)
--select depid,sum(salary) as total from temp group by rollup(depid)--grouping(xxx) 返回0或者1。如果列值为空,那么grouping()返回1;如果列值非空,那么返回0。grouping只能在使用rollup或cube的查询中使用    select grouping(depid), depid, sum(salary) as totalfrom tempgroup by rollup(depid);

可见,grouping(xxx)当xxx为空的时候返回1,当xxx不为空的时候返回0.

假如我需要最后一行depid为空显示‘总计’,可以通过下面的方法实现:

with temp as(select '10001' as depid, '12000' as salary, 'zhangsan' as n, '01' as jidfrom dualunion allselect '10001' as depid, '10000' as salary, 'lisi' as n, '02' as jidfrom dualunion allselect '10002' as depid, '1000' as salary, 'wangwu' as n, '01' as jidfrom dualunion allselect '10002' as depid, '2000' as salary, 'zhaoliu' as n, '03' as jidfrom dualunion allselect '10003' as depid, '15000' as salary, 'tianqi' as n, '01' as jidfrom dualunion allselect '10003' as depid, '15000' as salary, 'wangba' as n, '02' as jidfrom dualunion allselect '10004' as depid, '15000' as salary, 'jiudan' as n, '01' as jidfrom dual)--假如我需要最后一行depid为空显示‘总计’--可见列值为空返回1,列值不为空返回0
select decode(grouping(depid), 1, '总计', depid) as depid,sum(salary) as totalfrom tempgroup by rollup(depid);

另外一种实现方法:

with temp as(select '10001' as depid, '12000' as salary, 'zhangsan' as n, '01' as jidfrom dualunion allselect '10001' as depid, '10000' as salary, 'lisi' as n, '02' as jidfrom dualunion allselect '10002' as depid, '1000' as salary, 'wangwu' as n, '01' as jidfrom dualunion allselect '10002' as depid, '2000' as salary, 'zhaoliu' as n, '03' as jidfrom dualunion allselect '10003' as depid, '15000' as salary, 'tianqi' as n, '01' as jidfrom dualunion allselect '10003' as depid, '15000' as salary, 'wangba' as n, '02' as jidfrom dualunion allselect '10004' as depid, '15000' as salary, 'jiudan' as n, '01' as jidfrom dual)--假如我需要最后一行depid为空显示‘总计’--另一种方法:
select nvl(depid, '总计') as depid, sum(salary) as totalfrom tempgroup by rollup(depid);

【j】group by rollup():在分组统计并且需要小计、总结等功能时可以使用group by rollup()实现。
--rollup中列的顺序不同,则统计的结果不同。因为它是按列从右递减分组的。
--如 Group by  ROLLUP(A, B, C),首先会对(A、B、C)进行GROUP BY,然后对(A、B)进行GROUP BY,然后是(A)进行GROUP BY,最后对全表进行GROUP BY操作

我们先看看简单分组之后的结果:

with temp as(select '10001' as depid, '12000' as salary, 'zhangsan' as n, '01' as jidfrom dualunion allselect '10001' as depid, '10000' as salary, 'lisi' as n, '02' as jidfrom dualunion allselect '10002' as depid, '1000' as salary, 'wangwu' as n, '01' as jidfrom dualunion allselect '10002' as depid, '2000' as salary, 'zhaoliu' as n, '03' as jidfrom dualunion allselect '10003' as depid, '15000' as salary, 'tianqi' as n, '01' as jidfrom dualunion allselect '10003' as depid, '15000' as salary, 'wangba' as n, '02' as jidfrom dualunion allselect '10004' as depid, '15000' as salary, 'jiudan' as n, '01' as jidfrom dual)
select depid,sum(salary) as total from temp group by depid

然后,加上一个 group by rolluop(depid),

with temp as(select '10001' as depid, '12000' as salary, 'zhangsan' as n, '01' as jidfrom dualunion allselect '10001' as depid, '10000' as salary, 'lisi' as n, '02' as jidfrom dualunion allselect '10002' as depid, '1000' as salary, 'wangwu' as n, '01' as jidfrom dualunion allselect '10002' as depid, '2000' as salary, 'zhaoliu' as n, '03' as jidfrom dualunion allselect '10003' as depid, '15000' as salary, 'tianqi' as n, '01' as jidfrom dualunion allselect '10003' as depid, '15000' as salary, 'wangba' as n, '02' as jidfrom dualunion allselect '10004' as depid, '15000' as salary, 'jiudan' as n, '01' as jidfrom dual)--rollup指定一个列的时候,按depid分组之后有总计
--rollup中列的顺序不同,则统计的结果不同。因为它是按列从右递减分组的。
--如 Group by  ROLLUP(A, B, C),首先会对(A、B、C)进行GROUP BY,然后对(A、B)进行GROUP BY,然后是(A)进行GROUP BY,最后对全表进行GROUP BY操作
select nvl(depid,'总计'),sum(salary) as total from temp group by rollup(depid)

可见,在rollup加上一列的时候,最后一行出现了总计。

但是,当rollup()中指定多个列的时候,按第一个的分组都会有一个小计,如下:

with temp as(select '10001' as depid, '12000' as salary, 'zhangsan' as n, '01' as jidfrom dualunion allselect '10001' as depid, '10000' as salary, 'lisi' as n, '02' as jidfrom dualunion allselect '10002' as depid, '1000' as salary, 'wangwu' as n, '01' as jidfrom dualunion allselect '10002' as depid, '2000' as salary, 'zhaoliu' as n, '03' as jidfrom dualunion allselect '10003' as depid, '15000' as salary, 'tianqi' as n, '01' as jidfrom dualunion allselect '10003' as depid, '15000' as salary, 'wangba' as n, '02' as jidfrom dualunion allselect '10004' as depid, '15000' as salary, 'jiudan' as n, '01' as jidfrom dual)--rollup指定多个列的时候,按第一个的分组都会有一个小计
select decode(grouping(jid) + grouping(depid), 1, '小计', 2, '总计', jid) jid,depid,sum(salary) as totalfrom tempgroup by rollup(jid, depid)

可见,每一组Jid之后都出现了一个小计结果,最后一行也出现了总计结果,在实际工作中进行Oracle报表统计时相当有用。

但是,rollup()中指定多个列时,顺序不同,统计的结果也不同。

with temp as(select '10001' as depid, '12000' as salary, 'zhangsan' as n, '01' as jidfrom dualunion allselect '10001' as depid, '10000' as salary, 'lisi' as n, '02' as jidfrom dualunion allselect '10002' as depid, '1000' as salary, 'wangwu' as n, '01' as jidfrom dualunion allselect '10002' as depid, '2000' as salary, 'zhaoliu' as n, '03' as jidfrom dualunion allselect '10003' as depid, '15000' as salary, 'tianqi' as n, '01' as jidfrom dualunion allselect '10003' as depid, '15000' as salary, 'wangba' as n, '02' as jidfrom dualunion allselect '10004' as depid, '15000' as salary, 'jiudan' as n, '01' as jidfrom dual)select decode(grouping(jid) + grouping(depid), 1, '小计', 2, '总计', depid) depid,jid,sum(salary) as totalfrom tempgroup by rollup(depid,jid)

与上面一个示例比较,这里是每一组depid对应的后面都出现了小计结果,最后出现总计结果。

二、总结

本文是笔者在实际工作中,对常用到Oracle的一些函数进行总结和一些实践,仅供大家学习参考,希望能对大家的学习有所帮助!

Oracle工作中常用函数的总结相关推荐

  1. Hive-SQL工作中常用函数总结及案例实战

    目录 0 引 言 1 空字段赋值 2 时间类 3 条件判断 4 多行转一行(行转列) 5 一行变多行(列转行) 6 窗口函数 7 排名函数 8 json解析函数 9 url解析函数 10 小 结 0 ...

  2. oracle sql now函数,SQL Server,MySQL,Oracle,PostgreSQL中常用函数用法(1)日

    练习使用Hibernate没有用MySQL数据库,而是用了前不久接触的PostgreSQL,由于不同的数据对于相同的操作有各自的函数,MySQL的date_format(),在PostgreSQL中是 ...

  3. oracle中各种函数,oracle中常用函数大全

    1.数值型常用函数 函数 返回值 样例 显示 ceil(n) 大于或等于数值n的最小整数 select ceil(10.6) from dual; 11 floor(n) 小于等于数值n的最大整数 s ...

  4. TF:tensorflow框架中常用函数介绍—tf.Variable()和tf.get_variable()用法及其区别

    TF:tensorflow框架中常用函数介绍-tf.Variable()和tf.get_variable()用法及其区别 目录 tensorflow框架 tensorflow.Variable()函数 ...

  5. oracle 求时间差年,Oracle计算时间差常用函数

    两个Date类型字段:START_DATE,END_DATE,计算这两个日期的时间差(分别以天,小时,分钟,秒,毫秒): 天: sql;"> ROUND(TO_NUMBER(END_D ...

  6. 计算机应用常用的30个函数,Excel中常用函数的使用

    ISSN 1009-30" 咖船r Kno别b内e and伯叻肋叻电奠知识'i技术 V01.6,No.30,October20lO,pP.8523-8524E-mail:x8jl@cccc. ...

  7. 在工作中常用的sql语句

    这个是我以前在工作中常用的一些SQL语句,里面大部分都是从其他网站上找的,只有部分是自己在工作中解决问题事记录的,现在一起贴出来跟大家分享下,其实很多时候看看别人的经验总结和技术的理解,会使自己少走很 ...

  8. mysql中常用函数与存储过程的创建

    mysql中常用函数与存储过程的创建 常用函数汇总 数学函数 字符串函数 日期和时间函数 条件判断函数 系统函数 加密函数 其他函数 自定义函数 自定义变量的声明和赋值 基本语法 实例 存储过程 事务 ...

  9. 工作中常用的Stream集合处理

    前言:Java8的新特性主要是Lambda表达式和流,当流和Lambda表达式结合起来一起使用时,因为流申明式处理数据集合的特点,它允许把函数作为一个方法的参数,让我们的代码更优雅简洁. Java8最 ...

  10. oracle数据库计算时间差,Oracle计算时间差常用函数

    Oracle计算时间差常用函数 发布时间:2020-07-08 19:25:32 来源:51CTO 阅读:877 作者:18620626259 Oracle计算时间差常用函数 两个Date类型字段:S ...

最新文章

  1. Android Jetpack 组件之 Lifecycle源码
  2. H2O —— 宣称性能是 Nginx 2 倍的 HTTP 服务器
  3. 使用malloc初始化一个类和new初始化一个类的区别
  4. linux mysql安装 读写分离_linux下安装mysql-proxy 配置读写分离
  5. Android 底层驱动开发步骤——linux内核层、HAL层、JNI层
  6. storm java开发环境搭建,看这里!Storm【单机版】环境搭建
  7. Linux 命令之 iwconfig 命令-配置无线网络接口
  8. 【python】r+,w+ 全局变量
  9. js动态添加的元素,动作绑定
  10. linux文件共享加锁,Linux共享数据管理——文件锁定
  11. vSphere Replication:虚拟机的保护伞
  12. 矩形窗、汉明窗效果对比(matlab)
  13. 测试中 Fakes、Mocks 以及 Stubs 概念明晰
  14. linux服务器网卡极限速率,linux下简单限制网卡速度
  15. word英文大写问题解决方案
  16. matlab dx dy dt,dx/dt=y,dy/dt=-sinx,求大神帮忙编一个MATL? 爱问知识人
  17. 大连东软计算机专业全国排名,大连东软信息学院就业怎么样?全国前三,名不虚传!...
  18. Metis异常检测初体验
  19. 如何在C++中实现复数矩阵运算
  20. python办公自动化ppt_最全总结 | 聊聊 Python 办公自动化之 PPT(下)

热门文章

  1. 上古卷轴5python_python 基础(五)协程 —— 微线程 greenlet gevent
  2. 马尔科夫决策过程(MDP) : BlackJack问题(MC-ES)
  3. 自动驾驶 5-1 比例积分微分 (PID) 控制Lesson 1: Proportional-Integral-Derivative (PID) Control
  4. php闭包 js闭包,JavaScript闭包与PHP闭包的区别是什么?
  5. java复杂的代码做程序_摆脱复杂烧脑的程序代码,利用快速开发平台轻轻松松做软件...
  6. Wide Deep 模型详解
  7. 122.买卖股票的最佳时机II
  8. atoi()函数和stoi()函数
  9. 递归二叉树的序列打印
  10. Multi-class classification:One-vs-all