2015年Oracle认证考试辅导:110个oracle常用函数总结(4)
发布时间:2010/3/10 18:56:32 来源:城市学习网 编辑:MOON
61.MAX(DISTINCTALL)求最大值,ALL表示对所有的值求最大值,DISTINCT表示对不同的值求最大值,相同的只取一次SQL
DEPTNO COUNT(*) SUM(SAL)
- - -
10 3 8750
20 5 10875
30 6 9400
66.HAVING
对分组统计再加限制条件
SQL select deptno,count(*),sum(sal) from scott.emp group by deptno having count(*)=5;
DEPTNO COUNT(*) SUM(SAL)
- - -
20 5 10875
30 6 9400
SQL select deptno,count(*),sum(sal) from scott.emp having count(*)=5 group by deptno ;
DEPTNO COUNT(*) SUM(SAL)
- - -
20 5 10875
30 6 9400
67.ORDER BY
用于对查询到的结果进行排序输出
SQL select deptno,ename,sal from scott.emp order by deptno,sal desc;
DEPTNO ENAME SAL
10 KING 5000
10 CLARK 2450
10 MILLER 1300
20 SCOTT 3000
20 FORD 3000
20 JONES 2975
20 ADAMS 1100
20 SMITH 800
30 BLAKE 2850
30 ALLEN 1600
30 TURNER 1500
30 WARD 1250
30 MARTIN 1250
30 JAMES 950
68. pl/sql中的case语句
select (case when DUMMY=‘X‘ then 0 else 1 end) as flag from dual;
case的第1种用法:
case col when ‘a‘ then 1
when ‘b‘ then 2
else 0 end
这种用法跟decode一样没什么区别
case的第2种用法:
case when score 60 then ‘d‘
when score =60 and score 70 then ‘c‘
when score =70 and score 80 then ‘b‘
else ‘a‘ end
69.NVL(expr1, expr2) NVL(expr1, expr2)-expr1为NULL,返回expr2;不为NULL,返回expr1。注意两者的类型要一致NVL2 (expr1, expr2, expr3) -expr1不为NULL,返回expr2;为NULL,返回expr3。expr2和expr3类型不
同的话,expr3会转换为expr2的类型NULLIF (expr1, expr2) -相等返回NULL,不等返回expr1
Oracle分析函数参考手册
Oracle从8.1.6开始提供分析函数,分析函数用于计算基于组的某种聚合值,它和聚合函数的不同之
处是对于每个组返回多行,而聚合函数对于每个组只返回一行。
常用的分析函数如下所列:
row_number() over(partition by ... order by ...) rank() over(partition by ... order by ...) dense_rank() over(partition by ... order by ...) count() over(partition by ... order by ...) max() over(partition by ... order by ...) min() over(partition by ... order by ...) sum() over(partition by ... order by ...) avg() over(partition by ... order by ...) first_value() over(partition by ... order by ...) last_value() over(partition by ... order by ...) lag() over(partition by ... order by ...) lead() over(partition by ... order by ...)
下面例子中使用的表来自Oracle自带的HR用户下的表,如果没有安装该用户,可以在SYS用户下运行$ORACLE_HOME/demo/schema/human_resources/hr_main.sql来创建。
除本文内容外,你还可参考:ROLLUP与CUBE [url]http://xsb.itpub.net/post/419/29159[/url]分析函数使用例子介绍:[url]http://xsb.itpub.net/post/419/44634[/url]本文如果未指明,缺省是在HR用户下运行例子。
开窗函数的的理解:
开窗函数指定了分析函数工作的数据窗口大小,这个数据窗口大小可能会随着行的变化而变化,举例如下
over(order by salary) 按照salary排序进行累计,order by是个默认的开窗函数
over(partition by deptno)按照部门分区
over(order by salary range between 50 preceding and 150 following)
每行对应的数据窗口是之前行幅度值不超过50,之后行幅度值不超过150
over(order by salary rows between 50 preceding and 150 following)
每行对应的数据窗口是之前50行,之后150行
over(order by salary rows between unbounded preceding and unbounded following)
每行对应的数据窗口是从第一行到最后一行,等效:
over(order by salary range between unbounded preceding and unbounded following)
主要参考资料:《expert one-on-one》Tom Kyte《Oracle9i SQL Reference》第6章
70。AVG功能描述:用于计算一个组和数据窗口内表达式的平均值。
SAMPLE:下面的例子中列c_mavg计算员工表中每个员工的平均薪水报告,该平均值由当前员工和与之具有
相同经理的前一个和后一个三者的平均数得来;
SELECT manager_id, last_name, hire_date, salary,
AVG(salary) OVER (PARTITION BY manager_id ORDER BY hire_date
ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS c_mavg
FROM employees;
MANAGER_ID LAST_NAME HIRE_DATE SALARY C_MAVG
- -
100 Kochhar 21-SEP-89 17000 17000
100 De Haan 13-JAN-93 17000 15000
100 Raphaely 07-DEC-94 11000 11966.6667 100 Kaufling 01-MAY-95 7900 10633.3333 100 Hartstein 17-FEB-96 13000 9633.33333 100 Weiss 18-JUL-96 8000 11666.6667 100 Russell 01-OCT-96 14000 11833.3333
: