您的位置:首页 > 数据库 > Oracle

ORACLE分析函数的应用例子1 over partition by

2008-08-20 21:19 519 查看
用分析函数来得出的的工资 占 部门比 工资占 公司 比
--包括工名,部门 工资 需要得出部门工资的总数,某员工的工资 占所在部门总工资数的百分比,以及占总工资数的百分比



select deptno,

ename,

sal,

sum(sal) over(partition by deptno order by sal, ename) cum_sal,

round(100 * ratio_to_report(sal) over(partition by deptno), 1) pct_dept,

round(100 * ratio_to_report(sal) over(), 1) pct_overall

from emp

order by deptno, sal;

通用的程序ORACLE 9 以下数据库都可以用的

select emp.deptno,

emp.ename,

emp.sal,

sum(emp4.sal) cum_sal,

round(100 * emp.sal / emp2.sal_by_dept, 1) pct_dept,

round(100 * emp.sal / emp3.sal_overall, 1) pct_overall

from emp,

(select deptno, sum(sal) sal_by_dept from emp group by deptno) emp2,

(select sum(sal) sal_overall from emp) emp3,

emp emp4

where emp.deptno = emp2.deptno

and emp.deptno = emp4.deptno

and (emp.sal > emp4.sal or

(emp.sal = emp4.sal and emp.ename >= emp4.ename))

group by emp.deptno,

emp.ename,

emp.sal,

round(100 * emp.sal / emp2.sal_by_dept, 1),

round(100 * emp.sal / emp3.sal_overall, 1)

order by deptno, sal;

但是使用 分析函数 数据执行速度比较块 而且随着数据增多 速度成数量级别递增
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐