RATIO_TO_REPORT

RATIO_TO_REPORT() computes the ratio of a value to the sum of a set of values. You can perform reporting with: SUM(), AVG(), MAX(), MIN(),COUNT(), VARIANCE(), and STDDEV(). RATIO_TO_REPORT performs calculations across groups and partitions within groups.


CREATE TABLE EMP (EMPNO NUMBER(4) NOT NULL,
                  ENAME VARCHAR2(10),
                  JOB VARCHAR2(9),
                  SAL NUMBER(7, 2),
                  DEPTNO NUMBER(2));

INSERT INTO EMP VALUES (1, 'SMITH', 'CLERK',     800,    20);
INSERT INTO EMP VALUES (2, 'ALLEN', 'SALESMAN', 1600,    30);
INSERT INTO EMP VALUES (3, 'WARD',  'SALESMAN', 1250,    30);
INSERT INTO EMP VALUES (4, 'JONES', 'MANAGER',  2975,    20);
INSERT INTO EMP VALUES (5, 'MARTIN','SALESMAN', 1250,    30);
INSERT INTO EMP VALUES (6, 'BLAKE', 'MANAGER',  2850,    30);
INSERT INTO EMP VALUES (7, 'CLARK', 'MANAGER',  2850,    10);
INSERT INTO EMP VALUES (8, 'SCOTT', 'ANALYST',  3000,    20);
INSERT INTO EMP VALUES (9, 'KING',  'PRESIDENT',3000,    10);
INSERT INTO EMP VALUES (10,'TURNER','SALESMAN', 1500,    30);
INSERT INTO EMP VALUES (11,'ADAMS', 'CLERK',    1500,    20);

SQL> SELECT deptno, job, SUM(sal) AS prd_type_amount,
  2  RATIO_TO_REPORT(SUM(sal)) OVER (PARTITION BY deptno)
  3  FROM emp
  4  GROUP BY deptno, job
  5  ORDER BY deptno, job;

    DEPTNO JOB       PRD_TYPE_AMOUNT RATIO_TO_REPORT(SUM(SAL))OVER(PARTITIONBYDEPTNO)
---------- --------- ---------------------------------------------------------------
        10 MANAGER              2850                                      .487179487
        10 PRESIDENT            3000                                      .512820513
        20 ANALYST              3000                                      .362537764
        20 CLERK                2300                                      .277945619
        20 MANAGER              2975                                      .359516616
        30 MANAGER              2850                                      .337278107
        30 SALESMAN             5600                                      .662721893

7 rows selected.

SQL>
Home »
Oracle »
Analytical Functions » 

Related: