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

用分析函数来得出的的工资 占 部门比 工资占 公司 比
--包括工名,部门 工资 需要得出部门工资的总数,某员工的工资 占所在部门总工资数的百分比,以及占总工资数的百分比

 
  1. select deptno,
  2.        ename,
  3.        sal,
  4.        sum(sal) over(partition by deptno order by sal, ename) cum_sal,
  5.        round(100 * ratio_to_report(sal) over(partition by deptno), 1) pct_dept,
  6.        round(100 * ratio_to_report(sal) over(), 1) pct_overall
  7.   from emp
  8.  order by deptno, sal;

 通用的程序ORACLE 9 以下数据库都可以用的
  1. select emp.deptno,
  2.        emp.ename,
  3.        emp.sal,
  4.        sum(emp4.sal) cum_sal,
  5.        round(100 * emp.sal / emp2.sal_by_dept, 1) pct_dept,
  6.        round(100 * emp.sal / emp3.sal_overall, 1) pct_overall
  7.   from emp,
  8.        (select deptno, sum(sal) sal_by_dept from emp group by deptno) emp2,
  9.        (select sum(sal) sal_overall from emp) emp3,
  10.        emp emp4
  11.  where emp.deptno = emp2.deptno
  12.    and emp.deptno = emp4.deptno
  13.    and (emp.sal > emp4.sal or
  14.        (emp.sal = emp4.sal and emp.ename >= emp4.ename))
  15.  group by emp.deptno,
  16.           emp.ename,
  17.           emp.sal,
  18.           round(100 * emp.sal / emp2.sal_by_dept, 1),
  19.           round(100 * emp.sal / emp3.sal_overall, 1)
  20.  order by deptno, sal;

但是使用 分析函数 数据执行速度比较块 而且随着数据增多 速度成数量级别递增

你可能感兴趣的:(oracle,数据库,report)