zoukankan      html  css  js  c++  java
  • 分析函数Ratio_to_report分解

    分析函数Ratio_to_report( ) over()使用说明

    表中需要计算单项占比:比如单项在部门占比多少,单项在公司占比多少。特别是在财务单项计算,部门个人薪水计算上。

    Ratio_to_report() 括号中就是分子,over() 括号中就是分母,分母缺省就是整个占比。

    Ratio_to_report 一般结合partition by 使用。

    (一)

    举例子说明:

    表emp,dept,两表关联列为 deptno

    create,insert into 步骤省略。

    SQL> select * from emp;

    EMPNO DEPTNO SALARY

    ------------------------------------------------------------------------------ ----------

    100 2 55

    101 1 50

    102 2 60

    SQL> select * from dept;

    DEPTNO SUM_OF_SALARY

    --------------------------------------- -------------

    1 50

    2 115

    (二)脚本:

    sum(salary) 是对每个部门deptno求和,partition by 是对部门分区,分组。

    pct_dept 是每个员工salary对部门的占比;

    pct_overall 是每个员工salary对整个公司的占比;

    select empno,
    deptno,
    salary,
    sum(salary) over (partitionby deptnoorderby deptno) sum_deptno_salary,
    ROUND(100*ratio_to_report(salary)
    over (partitionby deptno),1) pct_dept,
    ROUND(100*ratio_to_report(salary)
    over(),1) pct_overall
    from emp
    orderby empno,deptno

    查询结果:

    EMPNO DEPTNO SALARY SUM_DEPTNO_SALARY PCT_DEPT PCT_OVERALL

    ------------------------------------------------------------------------------ --------------------------- ----------

    100 2 55 115 47.8 33.3

    101 1 50 50 100 30.3

    102 2 60 115 52.2 36.4

    (三)

    ratio_to_report分析函数是oracle 8i以后才有的。如果DATABASE 不支持。可以改写:

    select emp.empno,
    emp.deptno,
    emp.salary,
    emp2.a,
    round(100*emp.salary/emp2.a,1) pct_dept_zb,
    round(100*emp.salary/emp3.b,1) pct_overall_zb
    from emp,
    (select deptno,sum(salary) a from emp
    groupby deptno
    orderby deptno,a
    ) emp2,
    (selectsum(salary) b from emp
    orderby deptno
    ) emp3

    where emp.deptno=emp2.deptno

    groupby emp.empno,
    emp.deptno,
    emp.salary,
    emp2.a,
    round(100*emp.salary/emp2.a,1),
    round(100*emp.salary/emp3.b,1)
    orderby emp.empno,emp.deptno结果验证:

    EMPNO DEPTNO SALARY A PCT_DEPT_ZB PCT_OVERALL_ZB

    ------- --------------------------------------- ----------------

    100 2 55 115 47.8 33.3

    101 1 50 50 100 30.3

    102 2 60 115 52.2 36.4

  • 相关阅读:
    Oracle 11g导出空表
    如何破解ROS路由器禁用路由PPPOE拨号?
    Oracle入门心得(2)
    ORACLE了局,每天10问(五)
    若何消除Oracle数据库的安静隐患(3)
    Oracle入门心得(1)
    ORACLE用户自定义备份与恢复条记
    ORACLE成绩,每天10问(三)
    ORACLE问题,每天10问(一)
    怎样消除Oracle数据库的安全隐患(1)
  • 原文地址:https://www.cnblogs.com/sumsen/p/2524849.html
Copyright © 2011-2022 走看看