Oracle函数总结

目录

1、补位函数

2、分组排序函数

2.1、rank() over()

2.2、dense_rank() over()

2.3、row_number() over()

3、Trunc()函数

3.1、处理日期

3.2、处理数字

4、保留两位小数的函数

1、trunc()函数(不四舍五入)

2、round()函数(四舍五入)

3、TO_CHAR ('0.99','fm9999999990.00')(四舍五入)

5、lag()/lead() over()分析函数

5.1、lag() over()

5.2、lead() over()


1、补位函数

LPADRPAD分别是左补位和右补位。

示例:select rpad('123',5,'0') from dual;

结果为:12300(左补位同理)

2、分组排序函数

2.1、rank() over()

2.2、dense_rank() over()

2.3、row_number() over()

2.3.1、

select sjlx, row_number() over (partition by sjlx order by sjlx desc) rn from EVENT_INFO

2.3.2、

select sjlx, row_number() over (order by sjlx desc) rn from EVENT_INFO GROUP BY sjlx

2.3.1是先同组中编号再进行分组,2.3.2是先分组再编号

注意:1、partition by+分组字段;

           2、在使用 row_number() over()函数时候,over()里头的分组以及排序的执行晚于 where 、group by、  order by 的执行;

           3、rank() over()和dense_rank() over()都可以在进行排名的场景下使用。

3、Trunc()函数

3.1、处理日期

1、select trunc(sysdate) from dual             --2013-01-06 今天的日期为2013-01-06
2、select trunc(sysdate, 'mm') from dual   --2013-01-01 返回当月第一天.
3、select trunc(sysdate,'yy') from dual      --2013-01-01 返回当年第一天
4、select trunc(sysdate,'dd') from dual      --2013-01-06 返回当前年月日
5、select trunc(sysdate,'yyyy') from dual   --2013-01-01 返回当年第一天
6、select trunc(sysdate,'d') from dual        --2013-01-06 (星期天)返回当前星期的第一天
7、select trunc(sysdate, 'hh') from dual     --2013-01-06 17:00:00 当前时间为17:35 
8、select trunc(sysdate, 'mi') from dual     --2013-01-06 17:35:00 TRUNC()函数没有秒的精确

3.2、处理数字

TRUNC(number,num_digits) 
number 需要截尾取整的数字;num_digits 用于指定取整精度的数字,Num_digits 的默认值为 0。
TRUNC()函数截取时不进行四舍五入

1、select trunc(123.458) from dual --123
2、select trunc(123.458,1) from dual --123.4
3、select trunc(123.458,-1) from dual --120
4、select trunc(123.458,-4) from dual --0
5、select trunc(123.458,4) from dual --123.458

4、保留两位小数的函数

1、trunc()函数(四舍五入)

示例:select trunc(field_name,2) from table_name;去掉精度是不进行四舍五入

2、round()函数(四舍五入)

示例:select round(field_name,2) from table_name;

3、TO_CHAR ('0.99','fm9999999990.00')(四舍五入)

示例:select to_char (field_name,'fm9999999990.00') from table_name;

5、lag()/lead() over()分析函数

5.1、lag() over()

lag():把下一行的数据显示在当前行(LAG列),为null显示null

with tmp as(
select '1' id ,'aa' name ,'22' age from dual union all
select '2' id ,'bb' name ,'20' age from dual union all
select '3' id ,'CC' name ,'21' age from dual
)
select a.*,
       lead(age,1) over (order by id desc) lag,
  a.age - lead(age,1) over (order by id desc) lag1
from tmp a

5.2、lead() over()

lead():把上一个行的数据显示在当前行(LAG列),为null显示null

with tmp as(
select '1' id ,'aa' name ,'22' age from dual union all
select '2' id ,'bb' name ,'20' age from dual union all
select '3' id ,'CC' name ,'21' age from dual
)
select a.*,
       lag(age,1) over (order by id desc) lag,
  a.age - lag(age,1) over (order by id desc) lag1
from tmp a

 

参考出处:https://blog.csdn.net/houfengfei668/article/details/90706855

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值