网站首页 > 技术文章 正文
昨天分享了一篇基础SQL语句,结果大家反应太简单,太基础,太low了,好吧,今天分享常见一点、复杂一点的Oracle函数,其中的例句中有函数套函数,还算中等难度,共同学习,欢迎大家吐槽!
Oracle的函数有多少,心里话,我真不知道,我在银行的开发工作中常用的也就30个左右,听说有一百多个,这个大家去求证!今天要分享的是Java开发中常用的Oracle函数,包括字符串处理函数、求和函数、数学函数、拼接函数、转换函数、替代函数等,每一个函数给几个例子,方便理解学习。
1、nvl和nvl2函数
nvl函数如果查询的为空值,就显示后面的值,trim为去空格,分化为ltrim()和rtrim();
select user_name,nvl(trim(user_mail),'为空') user_mail from user_table;
select user_name,nvl(ltrim(user_mail),'为空') user_mail from user_table;
2、nvl2函数,如果为空显示第二个参数值,不为空显示第三个参数值,这个函数是nvl()函数的升级,更灵活一些。
select user_name,nvl2(Trim(user_mail),'不为空','为空') user_mail from user_table;
3、case...when的使用,case。。when算是语法,不是函数,因为下面要用到,这里说明一下。这里使用两种方法来写:
a、select user_id,user_name,(case when user_sex='1' then '男' when user_sex= '2' then '女' else user_sex end ) user_sex,user_mail from user_table;
b、select user_id,user_name,(case user_sex when'1' then '男' when '2' then '女' else user_sex end ) user_sex, user_mail from user_table;
4、sum()求和函数和count()求和函数,统计类函数在开发中经常使用,一般和round()配合
现在求男占比和女占比,这种在统计项目中经常使用到,如我现在做到的报表系统,要比这个还要复杂,数据量还要大,而且有时候是三四张表管理、、、
这里有两种写法,一种是使用别名,一种是不使用别名
a、select
count(*) tot_num,sum(case when user_name='bakehe' then 1 else 0 end) bakehe_num,sum(case when user_name='what' then 1 else 0 end) what_num,sum(case when user_sex='男' or user_sex='1' then 1 else 0 end) 男_num,sum(case when user_sex='男' or user_sex='1' then 1 else 0 end)/count(*)*100||'%' 男占比 ,sum(case when user_sex in ('女','2') then 1 else 0 end) 女_num,round(sum(case when user_sex in ('女','2') then 1 else 0 end)/count(*)*100,3)||'%' 女占比 ,sum(case when user_sex not in('男','女','1','2') then 1 else 0 end ) 中间_num,nvl(round(sum(case when user_sex not in('男','女','1','2') then 1 else 0 end )/count(*)*100,2),0)||'%' 中间占比,sum(case when user_age='高中一班'then 1 else 0 end ) 一班_num from user_table;
b、第二种写法,使用别名,同时使用临时表
select
D.tot_num,D.bakehe_num,D.what_num,D.男_num,round(D.男_num/D.tot_num,4)*100||'%' 男占比,D.女_num,round(D.女_num/D.tot_num,4)*100||'%' 女占比,D.中间_num,nvl(round(D.中间_num/D.tot_num,4)*100||'%',0) 中间占比,D.一班_num from
(select count(*) tot_num,sum(case when user_name='bakehe' then 1 else 0 end) bakehe_num,sum(case when user_name='what' then 1 else 0 end) what_num,sum(case when user_sex='男' or user_sex='1' then 1 else 0 end) 男_num,sum(case when user_sex in ('女','2') then 1else 0 end) 女_num,sum(case when user_sex not in('男','女','1','2') then 1 else 0 end ) 中间_num,sum(case when user_age='高中一班'then 1 else 0 end ) 一班_num from user_table ) D ;
5、拼接函数:拼接函数CONCAT()和||,个人比较喜欢||,但是效率好像concat()快一些!
select concat(CONCAT(user_name||'是'||user_age,'得')||(case user_sex when '1' then '男' when '2' then '女' else user_sex end),'生') 信息 from user_table;
6、截取函数:SUBSTR(),当作为查询条件的时候,截取函数和like有点相识。
select user_id,user_name,substr(user_age,1,2) 年级,substr(user_age,3,10) 班级,user_tel from user_table;
select * from user_table where substr(user_age,3,10)='二班'
select * from user_table where user_age like '%二班%'
7、时间函数,Oracle的时间函数非常强大,可以把字符串转换成时间格式,也可以把时间格式转换成字符串(to_char属于字符串处理函数)
select user_id,user_name,to_char(to_date(tm_smp,'yyyymmdd hh24miss'),'dd-mm-yyyy day year') tm_smp from user_table;
select user_id,user_name,to_char(SYSDATE,'yyyymmdd hh24miss') from user_table;
8、数学函数,Oracle提供了非常强大和繁多的数据计算函数,包括round(),ln(),abs(),acos(),cos(),ceil()等等。。
select user_id,user_name,tm_smp,
ceil( ( to_date(up_time,'yyyy-mm-dd hh24:mi:ss')-to_date(to_char(tm_smp),'yyyy-mm-dd hh24:mi:ss') )* 24 * 60 * 60) 交易用时,
floor( ( to_date(up_time,'yyyy-mm-dd hh24:mi:ss')-to_date(to_char(tm_smp),'yyyy-mm-dd hh24:mi:ss') ) ) 天,
mod(trunc(ceil( ( to_date(up_time,'yyyy-mm-dd hh24:mi:ss')-to_date(to_char(tm_smp),'yyyy-mm-dd hh24:mi:ss') )* 24 * 60 * 60)/3600),24) 小时,
floor(mod(ceil((to_date(up_time,'yyyy-mm-dd hh24:mi:ss')-to_date(to_char(tm_smp),'yyyy-mm-dd hh24:mi:ss'))* 24 * 60 * 60),3600)/60) 分,
mod(ceil((to_date(up_time,'yyyy-mm-dd hh24:mi:ss')-to_date(to_char(tm_smp),'yyyy-mm-dd hh24:mi:ss'))* 24 * 60 * 60),60) 秒
from user_table
9、数学函数中的三角求值,这里只简单写几个,大家想测试,可以自己写一些SQL。
select user_id,user_name,substr(tm_smp,8,10), cos(substr(tm_smp,8,10)) 余弦 ,
tan(substr(tm_smp,8,10)) 正切 from user_table;
注意:Oracle的函数非常之多,但是常用的基本就是十几个,一些简单的如:max(),min(),count(),upper(),instr(),replace()等等,大家可以私下写一写SQL验证一下。
非常欢迎大家关注我的个人微信号:thiscode
同样欢迎大家吐槽,监督!
猜你喜欢
- 2024-10-20 1 小时 SQL 极速入门(三)——分析函数
- 2024-10-20 oracle中常用的一些函数,巧用函数,减少代码量
- 2024-10-20 Oracle常用系统函数-日期和时间类函数
- 2024-10-20 oracle 函数decode用法 oracle的decode方法
- 2024-10-20 数据库笔试面试5——在Oracle中,函数的描述正确的是
- 2024-10-20 ORACLE 取两张表差值函数 minus oracle查询两个表中相同数据
- 2024-10-20 Oracle行转列、列转行函数 oracle12c行转列函数
- 2024-10-20 用ORACLE分析函数decode实现行列转换
- 2024-10-20 2周零基础搞定SQL——窗口函数 sql 窗口函数执行顺序
- 2024-10-20 Oracle数据库中几种非常有用的函数(with……as等)
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- 前端设计模式 (75)
- 前端性能优化 (51)
- 前端模板 (66)
- 前端跨域 (52)
- 前端md5加密 (49)
- 前端路由 (55)
- 前端数组 (65)
- 前端定时器 (47)
- 前端懒加载 (45)
- 前端接口 (46)
- Oracle RAC (73)
- oracle恢复 (76)
- oracle 删除表 (48)
- oracle 用户名 (74)
- oracle 工具 (55)
- oracle 内存 (50)
- oracle 导出表 (57)
- oracle查询数据库 (45)
- oracle约束 (46)
- oracle 中文 (51)
- oracle链接 (47)
- oracle的函数 (57)
- mac oracle (47)
- 前端调试 (52)
- 前端登录页面 (48)
本文暂时没有评论,来添加一个吧(●'◡'●)