oracle完整函数全套汇编(分析函数聚合函数转换函数日期型函数字符型函数数值型函数其他函数).doc

上传人:小** 文档编号:3009506 上传时间:2020-06-21 格式:DOC 页数:101 大小:114.68KB
返回 下载 相关 举报
oracle完整函数全套汇编(分析函数聚合函数转换函数日期型函数字符型函数数值型函数其他函数).doc_第1页
第1页 / 共101页
oracle完整函数全套汇编(分析函数聚合函数转换函数日期型函数字符型函数数值型函数其他函数).doc_第2页
第2页 / 共101页
点击查看更多>>
资源描述

《oracle完整函数全套汇编(分析函数聚合函数转换函数日期型函数字符型函数数值型函数其他函数).doc》由会员分享,可在线阅读,更多相关《oracle完整函数全套汇编(分析函数聚合函数转换函数日期型函数字符型函数数值型函数其他函数).doc(101页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、-!oracle函数大全(分析函数,聚合函数,转换函数,日期型函数,字符型函数,数值型函数,其他函数)oracle函数大全1oracle分析函数-SQL*PLUS环境1oracle 10g函数大全-聚合函数19oracle 10g函数大全-转换函数23oracle 10g函数大全-日期型函数40oracle 10g函数大全-字符型函数45oracle 10g函数大全-数值型函数55oracle 10g函数大全-其他函数58oracle分析函数-SQL*PLUS环境一、总体介绍1.1. 分析函数如何工作语法 FUNCTION_NAME(,) OVER ( ORDER BY 表达式 ) PARTI

2、TION子句 ORDER BY子句 WINDOWING子句 缺省时相当于RANGE UNBOUNDED PRECEDING 1. 值域窗(RANGE WINDOW) RANGE N PRECEDING 仅对数值或日期类型有效,选定窗为排序后当前行之前,某列(即排序列)值大于/小于(当前行该列值 /+ N)的所有行,因此与ORDER BY子句有关系。 2. 行窗(ROW WINDOW) ROWS N PRECEDING 选定窗为当前行及之前N行。 还可以加上BETWEEN AND 形式,例如RANGE BETWEEN m PRECEDING AND n FOLLOWING 函数 AVG( eXP

3、r) 一组或选定窗中表达式的平均值 CORR(expr, expr) 即COVAR_POP(exp1,exp2) / (STDDEV_POP(expr1) * STDDEV_POP(expr2),两个表达式的互相关,-1(反相关) 1(正相关),0表示不相关 COUNT( ) 计数 COVAR_POP(expr, expr) 总体协方差 COVAR_SAMP(expr, expr) 样本协方差 CUME_DIST 累积分布,即行在组中的相对位置,返回0 1 DENSE_RANK 行的相对排序(与ORDER BY搭配),相同的值具有一样的序数(NULL计为相同),并不留空序数 FIRST_VAL

4、UE 一个组的第一个值 LAG(expr, , ) 访问之前的行,OFFSET是缺省为1 的正数,表示相对行数,DEFAULT是当超出选定窗范围时的返回值(如第一行不存在之前行) LAST_VALUE 一个组的最后一个值 LEAD(expr, , ) 访问之后的行,OFFSET是缺省为1 的正数,表示相对行数,DEFAULT是当超出选定窗范围时的返回值(如最后行不存在之前行) MAX(expr) 最大值 MIN(expr) 最小值 NTILE(expr) 按表达式的值和行在组中的位置编号,如表达式为4,则组分4份,分别为1 4的值,而不能等分则多出的部分在值最小的那组 PERCENT_RANK

5、 类似CUME_DIST,1/(行的序数 - 1) RANK 相对序数,答应并列,并空出随后序号 RATIO_TO_REPORT(expr) 表达式值 / SUM(表达式值) ROW_NUMBER 排序的组中行的偏移 STDDEV(expr) 标准差 STDDEV_POP(expr) 总体标准差 STDDEV_SAMP(expr) 样本标准差 SUM(expr) 合计 VAR_POP(expr) 总体方差 VAR_SAMP(expr) 样本方差 VARIANCE(expr) 方差 REGR_ xxxx(expr, expr) 线性回归函数 REGR_SLOPE:返回斜率,等于COVAR_POP

6、(expr1, expr2) / VAR_POP(expr2)REGR_INTERCEPT:返回回归线的y截距,等于AVG(expr1) - REGR_SLOPE(expr1, expr2) * AVG(expr2)REGR_COUNT:返回用于填充回归线的非空数字对的数目REGR_R2:返回回归线的决定系数,计算式为:If VAR_POP(expr2) = 0 then return NULLIf VAR_POP(expr1) = 0 and VAR_POP(expr2) != 0 then return 1If VAR_POP(expr1) 0 and VAR_POP(expr2 != 0

7、 then return POWER(CORR(expr1,expr),2)REGR_AVGX:计算回归线的自变量(expr2)的平均值,去掉了空对(expr1, expr2)后,等于AVG(expr2)REGR_AVGY:计算回归线的应变量(expr1)的平均值,去掉了空对(expr1, expr2)后,等于AVG(expr1)REGR_SXX: 返回值等于REGR_COUNT(expr1, expr2) * VAR_POP(expr2)REGR_SYY: 返回值等于REGR_COUNT(expr1, expr2) * VAR_POP(expr1)REGR_SXY: 返回值等于REGR_CO

8、UNT(expr1, expr2) * COVAR_POP(expr1, expr2)首先:创建表及接入测试数据create table students(id number(15,0),area varchar2(10),stu_type varchar2(2),score number(20,2);insert into students values(1, 111, g, 80 );insert into students values(1, 111, j, 80 );insert into students values(1, 222, g, 89 );insert into stud

9、ents values(1, 222, g, 68 );insert into students values(2, 111, g, 80 );insert into students values(2, 111, j, 70 );insert into students values(2, 222, g, 60 );insert into students values(2, 222, j, 65 );insert into students values(3, 111, g, 75 );insert into students values(3, 111, j, 58 );insert i

10、nto students values(3, 222, g, 58 );insert into students values(3, 222, j, 90 );insert into students values(4, 111, g, 89 );insert into students values(4, 111, j, 90 );insert into students values(4, 222, g, 90 );insert into students values(4, 222, j, 89 );commit;二、具体应用:1、分组求和:1.2. GROUP BY子句 1.2.1.

11、GROUPING SETSselect id,area,stu_type,sum(score) score from studentsgroup by grouping sets(id,area,stu_type),(id,area),id)order by id,area,stu_type;/*-理解grouping setsselect a, b, c, sum( d ) from tgroup by grouping sets ( a, b, c )等效于select * from (select a, null, null, sum( d ) from t group by aunio

12、n allselect null, b, null, sum( d ) from t group by b union allselect null, null, c, sum( d ) from t group by c )*/1.2.2. ROLLUPselect id,area,stu_type,sum(score) score from studentsgroup by rollup(id,area,stu_type)order by id,area,stu_type;1.2.3. rollupselect a, b, c, sum( d )from tgroup by rollup(

13、a, b, c);等效于select * from (select a, b, c, sum( d ) from t group by a, b, c union allselect a, b, null, sum( d ) from t group by a, bunion allselect a, null, null, sum( d ) from t group by aunion allselect null, null, null, sum( d ) from t)*/1.2.4. CUBEselect id,area,stu_type,sum(score) score from s

14、tudentsgroup by cube(id,area,stu_type)order by id,area,stu_type;/*-理解cubeselect a, b, c, sum( d ) from tgroup by cube( a, b, c)等效于select a, b, c, sum( d ) from tgroup by grouping sets( ( a, b, c ), ( a, b ), ( a ), ( b, c ), ( b ), ( a, c ), ( c ), () )*/1.2.5. GROUPING/*从上面的结果中我们很容易发现,每个统计数据所对应的行都会

15、出现null,如何来区分到底是根据那个字段做的汇总呢,grouping函数判断是否合计列!*/select decode(grouping(id),1,all id,id) id,decode(grouping(area),1,all area,to_char(area) area,decode(grouping(stu_type),1,all_stu_type,stu_type) stu_type,sum(score) scorefrom studentsgroup by cube(id,area,stu_type)order by id,area,stu_type; 1.3. OVER()

16、函数的使用1.3.1. 统计名次1.3.1.1. DENSE_RANK(),允许并列名次、名次不间断,如122344456将score按ID分组排名:dense_rank() over(partition by id order by score desc)将score不分组排名:dense_rank() over(order by score desc)select id,area,score,dense_rank() over(partition by id order by score desc) 分组id排序,dense_rank() over(order by score desc)

17、 不分组排序from students order by id,area;1.3.1.2. ROW_NUMBER(),不允许并列名次、相同值名次不重复,结果如123456将score按ID分组排名:row_number() over(partition by id order by score desc)将score不分组排名:row_number() over(order by score desc)select id,area,score,row_number() over(partition by id order by score desc) 分组id排序,row_number() o

18、ver(order by score desc) 不分组排序from students order by id,area;1.3.1.3. rank(),允许并列名次、复制名次自动空缺,结果如12245558将score按ID分组排名:rank() over(partition by id order by score desc)将score不分组排名:rank() over(order by score desc)select id,area,score,rank() over(partition by id order by score desc) 分组id排序,rank() over(o

19、rder by score desc) 不分组排序from students order by id,area;1.3.1.4. cume_dist(),名次分析-最大排名/总个数 函数:cume_dist() over(order by id)select id,area,score,cume_dist() over(order by id) a, -按ID最大排名/总个数 cume_dist() over(partition by id order by score desc) b, -ID分组中,scroe最大排名值/本组总个数row_number() over (order by id

20、) 记录号from students order by id,area;1.3.1.5. cume_dist(),允许并列名次、复制名次自动空缺,取并列后较大名次,结果如22355778将score按ID分组排名:cume_dist() over(partition by id order by score desc)*sum(1) over(partition by id)将score不分组排名:cume_dist() over(order by score desc)*sum(1) over()select id,area,score,sum(1) over() as 总数,sum(1)

21、over(partition by id) as 分组个数,(cume_dist() over(partition by id order by score desc)*(sum(1) over(partition by id) 分组id排序,(cume_dist() over(order by score desc)*(sum(1) over() 不分组排序from students order by id,area1.3.1.6. sum(),max(),avg(),RATIO_TO_REPORT()分组统计select id,area,sum(1) over() as 总记录数, sum

22、(1) over(partition by id) as 分组记录数,sum(score) over() as 总计 , sum(score) over(partition by id) as 分组求和,sum(score) over(order by id) as 分组连续求和,sum(score) over(partition by id,area) as 分组ID和area求和,sum(score) over(partition by id order by area) as 分组ID并连续按area求和,max(score) over() as 最大值,max(score) over(

23、partition by id) as 分组最大值,max(score) over(order by id) as 分组连续最大值,max(score) over(partition by id,area) as 分组ID和area求最大值,max(score) over(partition by id order by area) as 分组ID并连续按area求最大值,avg(score) over() as 所有平均,avg(score) over(partition by id) as 分组平均,avg(score) over(order by id) as 分组连续平均,avg(sc

24、ore) over(partition by id,area) as 分组ID和area平均,avg(score) over(partition by id order by area) as 分组ID并连续按area平均,RATIO_TO_REPORT(score) over() as 占所有%,RATIO_TO_REPORT(score) over(partition by id) as 占分组%,score from students;3、LAG(COL,n,default)、LEAD(OL,n,default) -取前后边N条数据取前面记录的值:lag(score,n,x) over(

25、order by id)取后面记录的值:lead(score,n,x) over(order by id) 参数:n表示移动N条记录,X表示不存在时填充值,iD表示排序列select id,lag(score,1,0) over(order by id) lg,score from students;select id,lead(score,1,0) over(order by id) lg,score from students;4、FIRST_VALUE()、LAST_VALUE()取第起始1行值:first_value(score,n) over(order by id)取第最后1行值:

26、LAST_value(score,n) over(order by id)select id,first_value(score) over(order by id) fv,score from students;select id,last_value(score) over(order by id) fv,score from students;sum(.) over .【功能】连续求和分析函数【参数】具体参示例【说明】Oracle分析函数NC示例:select bdcode,sum(1) over(order by bdcode) aa from bd_bdinfo 【示例】1.原表信息

27、: SQL break on deptno skip 1 - 为效果更明显,把不同部门的数据隔段显示。SQL select deptno,ename,sal 2 from emp 3 order by deptno;DEPTNO ENAME SAL- - - 10 CLARK 2450 KING 5000 MILLER 1300 20 SMITH 800 ADAMS 1100 FORD 3000 SCOTT 3000 JONES 2975 30 ALLEN 1600 BLAKE 2850 MARTIN 1250 JAMES 950 TURNER 1500 WARD 12502.先来一个简单的,

28、注意over(.)条件的不同,使用 sum(sal) over (order by ename). 查询员工的薪水“连续”求和,注意over (order by ename)如果没有order by 子句,求和就不是“连续”的,放在一起,体会一下不同之处:SQL select deptno,ename,sal, 2 sum(sal) over (order by ename) 连续求和, 3 sum(sal) over () 总和, - 此处sum(sal) over () 等同于sum(sal) 4 100*round(sal/sum(sal) over (),4) 份额(%) 5 from

29、 emp 6 /DEPTNO ENAME SAL 连续求和 总和 份额(%)- - - - - - 20 ADAMS 1100 1100 29025 3.79 30 ALLEN 1600 2700 29025 5.51 30 BLAKE 2850 5550 29025 9.82 10 CLARK 2450 8000 29025 8.44 20 FORD 3000 11000 29025 10.34 30 JAMES 950 11950 29025 3.27 20 JONES 2975 14925 29025 10.25 10 KING 5000 19925 29025 17.23 30 MAR

30、TIN 1250 21175 29025 4.31 10 MILLER 1300 22475 29025 4.48 20 SCOTT 3000 25475 29025 10.34 20 SMITH 800 26275 29025 2.76 30 TURNER 1500 27775 29025 5.17 30 WARD 1250 29025 29025 4.313.使用子分区查出各部门薪水连续的总和。注意按部门分区。注意over(.)条件的不同,sum(sal) over (partition by deptno order by ename) 按部门“连续”求总和sum(sal) over (

31、partition by deptno) 按部门求总和sum(sal) over (order by deptno,ename) 不按部门“连续”求总和sum(sal) over () 不按部门,求所有员工总和,效果等同于sum(sal)。SQL select deptno,ename,sal, 2 sum(sal) over (partition by deptno order by ename) 部门连续求和,-各部门的薪水连续求和 3 sum(sal) over (partition by deptno) 部门总和, - 部门统计的总和,同一部门总和不变 4 100*round(sal/

32、sum(sal) over (partition by deptno),4) 部门份额(%), 5 sum(sal) over (order by deptno,ename) 连续求和, -所有部门的薪水连续求和 6 sum(sal) over () 总和, - 此处sum(sal) over () 等同于sum(sal),所有员工的薪水总和 7 100*round(sal/sum(sal) over (),4) 总份额(%) 8 from emp 9 /DEPTNO ENAME SAL 部门连续求和 部门总和 部门份额(%) 连续求和 总和 总份额(%)- - - - - - - - -10

33、 CLARK 2450 2450 8750 28 2450 29025 8.44 KING 5000 7450 8750 57.14 7450 29025 17.23 MILLER 1300 8750 8750 14.86 8750 29025 4.4820 ADAMS 1100 1100 10875 10.11 9850 29025 3.79 FORD 3000 4100 10875 27.59 12850 29025 10.34 JONES 2975 7075 10875 27.36 15825 29025 10.25 SCOTT 3000 10075 10875 27.59 18825

34、29025 10.34 SMITH 800 10875 10875 7.36 19625 29025 2.7630 ALLEN 1600 1600 9400 17.02 21225 29025 5.51 BLAKE 2850 4450 9400 30.32 24075 29025 9.82 JAMES 950 5400 9400 10.11 25025 29025 3.27 MARTIN 1250 6650 9400 13.3 26275 29025 4.31 TURNER 1500 8150 9400 15.96 27775 29025 5.17 WARD 1250 9400 9400 13

35、.3 29025 29025 4.314.来一个综合的例子,求和规则有按部门分区的,有不分区的例子SQL select deptno,ename,sal,sum(sal) over (partition by deptno order by sal) dept_sum, 2 sum(sal) over (order by deptno,sal) sum 3 from emp;DEPTNO ENAME SAL DEPT_SUM SUM- - - - - 10 MILLER 1300 1300 1300 CLARK 2450 3750 3750 KING 5000 8750 8750 20 SMI

36、TH 800 800 9550 ADAMS 1100 1900 10650 JONES 2975 4875 13625 SCOTT 3000 10875 19625 FORD 3000 10875 19625 30 JAMES 950 950 20575 WARD 1250 3450 23075 MARTIN 1250 3450 23075 TURNER 1500 4950 24575 ALLEN 1600 6550 26175 BLAKE 2850 9400 290255.来一个逆序的,即部门从大到小排列,部门里各员工的薪水从高到低排列,累计和的规则不变。SQL select deptno,

37、ename,sal, 2 sum(sal) over (partition by deptno order by deptno desc,sal desc) dept_sum, 3 sum(sal) over (order by deptno desc,sal desc) sum 4 from emp;DEPTNO ENAME SAL DEPT_SUM SUM- - - - - 30 BLAKE 2850 2850 2850 ALLEN 1600 4450 4450 TURNER 1500 5950 5950 WARD 1250 8450 8450 MARTIN 1250 8450 8450

38、JAMES 950 9400 9400 20 SCOTT 3000 6000 15400 FORD 3000 6000 15400 JONES 2975 8975 18375 ADAMS 1100 10075 19475 SMITH 800 10875 20275 10 KING 5000 5000 25275 CLARK 2450 7450 27725 MILLER 1300 8750 290256.体会:在. from emp;后面不要加order by 子句,使用的分析函数的(partition by deptno order by sal)里已经有排序的语句了,如果再在句尾添加排序子句

39、,一致倒罢了,不一致,结果就令人费劲了。如:SQL select deptno,ename,sal,sum(sal) over (partition by deptno order by sal) dept_sum, 2 sum(sal) over (order by deptno,sal) sum 3 from emp 4 order by deptno desc;DEPTNO ENAME SAL DEPT_SUM SUM- - - - - 30 JAMES 950 950 20575 WARD 1250 3450 23075 MARTIN 1250 3450 23075 TURNER 1500 4950

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 技术资料 > 其他杂项

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁