Oracle PL/SQL/Analytical Functions/RATIO TO REPORT — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 16:45, 26 мая 2010
Содержание
Notice that the PORTION column adds up to 100%
<source lang="sql">
SQL> -- create demo table SQL> create table Employee(
2 empno Number(3) NOT NULL, -- Employee ID 3 ename VARCHAR2(10 BYTE), -- Employee Name 4 hireDate DATE, -- Date Employee Hired 5 orig_salary Number(8,2), -- Orignal Salary 6 curr_salary Number(8,2), -- Current Salary 7 region VARCHAR2(1 BYTE) -- Region where employeed 8 ) 9 /
Table created. SQL> SQL> SQL> -- prepare data for employee table SQL> insert into Employee(empno, ename, hireDate, orig_salary, curr_salary, region)
2 values(122,"Alison",to_date("19960321","YYYYMMDD"), 45000, 48000, "E") 3 /
1 row created. SQL> insert into Employee(empno, ename, hireDate, orig_salary, curr_salary, region)
2 values(123, "James",to_date("19781212","YYYYMMDD"), 23000, 32000, "W") 3 /
1 row created. SQL> insert into Employee(empno, ename, hireDate, orig_salary, curr_salary, region)
2 values(104,"Celia",to_date("19821024","YYYYMMDD"), 53000, 58000, "E") 3 /
1 row created. SQL> insert into Employee(empno, ename, hireDate, orig_salary, curr_salary, region)
2 values(105,"Robert",to_date("19840115","YYYYMMDD"), 31000, 36000, "W") 3 /
1 row created. SQL> insert into Employee(empno, ename, hireDate, orig_salary, curr_salary, region)
2 values(116,"Linda", to_date("19870730","YYYYMMDD"), 43000, 53000, "E") 3 /
1 row created. SQL> insert into Employee(empno, ename, hireDate, orig_salary, curr_salary, region)
2 values(117,"David", to_date("19901231","YYYYMMDD"), 78000, 85000, "W") 3 /
1 row created. SQL> insert into Employee(empno, ename, hireDate, orig_salary, curr_salary, region)
2 values(108,"Jode", to_date("19960917","YYYYMMDD"), 21000, 29000, "E") 3 /
1 row created. SQL> SQL> select * from employee;
EMPNO ENAME HIREDATE ORIG_SALARY CURR_SALARY R
---------- --------- ----------- ----------- -
122 Alison 21-MAR-96 45000 48000 E 123 James 12-DEC-78 23000 32000 W 104 Celia 24-OCT-82 53000 58000 E 105 Robert 15-JAN-84 31000 36000 W 116 Linda 30-JUL-87 43000 53000 E 117 David 31-DEC-90 78000 85000 W 108 Jode 17-SEP-96 21000 29000 E
7 rows selected. SQL> SQL> -- Notice that the PORTION column adds up to 100%: SQL> SQL> COLUMN total FORMAT 9.9999; SQL> SQL> SELECT sum(o.portion) Total
2 FROM 3 (SELECT i.ename, i.curr_salary, 4 i.curr_salary/SUM(i.curr_salary) OVER() Portion 5 FROM employee i 6 ORDER BY i.curr_salary) o; TOTAL
1.0000
SQL> SQL> SQL> SQL> -- clean the table SQL> drop table Employee
2 /
Table dropped. SQL> SQL> SQL>
</source>
Portion amounts add to 1.000 in each region
<source lang="sql">
SQL> -- create demo table SQL> create table Employee(
2 empno Number(3) NOT NULL, -- Employee ID 3 ename VARCHAR2(10 BYTE), -- Employee Name 4 hireDate DATE, -- Date Employee Hired 5 orig_salary Number(8,2), -- Orignal Salary 6 curr_salary Number(8,2), -- Current Salary 7 region VARCHAR2(1 BYTE) -- Region where employeed 8 ) 9 /
Table created. SQL> SQL> -- prepare data for employee table SQL> insert into Employee(empno, ename, hireDate, orig_salary, curr_salary, region)
2 values(122,"Alison",to_date("19960321","YYYYMMDD"), 45000, 48000, "E") 3 /
1 row created. SQL> insert into Employee(empno, ename, hireDate, orig_salary, curr_salary, region)
2 values(123, "James",to_date("19781212","YYYYMMDD"), 23000, 32000, "W") 3 /
1 row created. SQL> insert into Employee(empno, ename, hireDate, orig_salary, curr_salary, region)
2 values(104,"Celia",to_date("19821024","YYYYMMDD"), 53000, 58000, "E") 3 /
1 row created. SQL> insert into Employee(empno, ename, hireDate, orig_salary, curr_salary, region)
2 values(105,"Robert",to_date("19840115","YYYYMMDD"), 31000, 36000, "W") 3 /
1 row created. SQL> insert into Employee(empno, ename, hireDate, orig_salary, curr_salary, region)
2 values(116,"Linda", to_date("19870730","YYYYMMDD"), 43000, 53000, "E") 3 /
1 row created. SQL> insert into Employee(empno, ename, hireDate, orig_salary, curr_salary, region)
2 values(117,"David", to_date("19901231","YYYYMMDD"), 78000, 85000, "W") 3 /
1 row created. SQL> insert into Employee(empno, ename, hireDate, orig_salary, curr_salary, region)
2 values(108,"Jode", to_date("19960917","YYYYMMDD"), 21000, 29000, "E") 3 /
1 row created. SQL> SQL> select * from employee;
EMPNO ENAME HIREDATE ORIG_SALARY CURR_SALARY R
---------- --------- ----------- ----------- -
122 Alison 21-MAR-96 45000 48000 E 123 James 12-DEC-78 23000 32000 W 104 Celia 24-OCT-82 53000 58000 E 105 Robert 15-JAN-84 31000 36000 W 116 Linda 30-JUL-87 43000 53000 E 117 David 31-DEC-90 78000 85000 W 108 Jode 17-SEP-96 21000 29000 E
7 rows selected. SQL> SQL> -- Portion amounts add to 1.000 in each region SQL> SQL> SELECT ename, curr_salary, region,
2 curr_salary/SUM(curr_salary) OVER(PARTITION BY Region) 3 Portion, 4 RATIO_TO_REPORT(curr_salary) OVER(PARTITION BY Region) 5 Portion2 6 FROM employee 7 UNION 8 SELECT null, TO_NUMBER(null), region, sum(P1), sum(p2) 9 FROM 10 (SELECT ename, curr_salary, region, 11 curr_salary/SUM(curr_salary) OVER(PARTITION BY Region) P1, 12 RATIO_TO_REPORT(curr_salary) OVER(PARTITION BY Region) P2 13 FROM employee) 14 GROUP BY region 15 ORDER BY 3,2;
ENAME CURR_SALARY R PORTION PORTION2
----------- - -------- --------
Jode 29000 E .1543 .1543 Alison 48000 E .2553 .2553 Linda 53000 E .2819 .2819 Celia 58000 E .3085 .3085
E 1.0000 1.0000
James 32000 W .2092 .2092 Robert 36000 W .2353 .2353 David 85000 W .5556 .5556
W 1.0000 1.0000
9 rows selected. SQL> SQL> SQL> -- clean the table SQL> drop table Employee
2 /
Table dropped. SQL> SQL> SQL> SQL>
</source>
RATIO_TO_REPORT, break on and compute
<source lang="sql">
SQL> -- create demo table SQL> create table Employee(
2 empno Number(3) NOT NULL, -- Employee ID 3 ename VARCHAR2(10 BYTE), -- Employee Name 4 hireDate DATE, -- Date Employee Hired 5 orig_salary Number(8,2), -- Orignal Salary 6 curr_salary Number(8,2), -- Current Salary 7 region VARCHAR2(1 BYTE) -- Region where employeed 8 ) 9 /
Table created. SQL> SQL> -- prepare data for employee table SQL> insert into Employee(empno, ename, hireDate, orig_salary, curr_salary, region)
2 values(122,"Alison",to_date("19960321","YYYYMMDD"), 45000, 48000, "E") 3 /
1 row created. SQL> insert into Employee(empno, ename, hireDate, orig_salary, curr_salary, region)
2 values(123, "James",to_date("19781212","YYYYMMDD"), 23000, 32000, "W") 3 /
1 row created. SQL> insert into Employee(empno, ename, hireDate, orig_salary, curr_salary, region)
2 values(104,"Celia",to_date("19821024","YYYYMMDD"), 53000, 58000, "E") 3 /
1 row created. SQL> insert into Employee(empno, ename, hireDate, orig_salary, curr_salary, region)
2 values(105,"Robert",to_date("19840115","YYYYMMDD"), 31000, 36000, "W") 3 /
1 row created. SQL> insert into Employee(empno, ename, hireDate, orig_salary, curr_salary, region)
2 values(116,"Linda", to_date("19870730","YYYYMMDD"), 43000, 53000, "E") 3 /
1 row created. SQL> insert into Employee(empno, ename, hireDate, orig_salary, curr_salary, region)
2 values(117,"David", to_date("19901231","YYYYMMDD"), 78000, 85000, "W") 3 /
1 row created. SQL> insert into Employee(empno, ename, hireDate, orig_salary, curr_salary, region)
2 values(108,"Jode", to_date("19960917","YYYYMMDD"), 21000, 29000, "E") 3 /
1 row created. SQL> SQL> select * from employee;
EMPNO ENAME HIREDATE ORIG_SALARY CURR_SALARY R
---------- --------- ----------- ----------- -
122 Alison 21-MAR-96 45000 48000 E 123 James 12-DEC-78 23000 32000 W 104 Celia 24-OCT-82 53000 58000 E 105 Robert 15-JAN-84 31000 36000 W 116 Linda 30-JUL-87 43000 53000 E 117 David 31-DEC-90 78000 85000 W 108 Jode 17-SEP-96 21000 29000 E
7 rows selected. SQL> SQL> SQL> BREAK ON region SQL> SQL> COMPUTE sum of portion ON region SQL> SQL> SELECT ename, curr_salary, region,
2 curr_salary/SUM(curr_salary) OVER(PARTITION BY Region) 3 Portion, 4 RATIO_TO_REPORT(curr_salary) OVER(PARTITION BY Region) 5 Portion2 6 FROM employee 7 ORDER BY region, curr_salary;
ENAME CURR_SALARY R PORTION PORTION2
----------- - -------- --------
Jode 29000 E .1543 .1543 Alison 48000 .2553 .2553 Linda 53000 .2819 .2819 Celia 58000 .3085 .3085
* -------- s 1.0000
James 32000 W .2092 .2092 Robert 36000 .2353 .2353 David 85000 .5556 .5556
* -------- s 1.0000
7 rows selected. SQL> SQL> SQL> SQL> SQL> -- clean the table SQL> drop table Employee
2 /
Table dropped. SQL> SQL> SQL>
</source>
RATIO_TO_REPORT can easily be partioned as well
<source lang="sql">
SQL> -- create demo table SQL> create table Employee(
2 empno Number(3) NOT NULL, -- Employee ID 3 ename VARCHAR2(10 BYTE), -- Employee Name 4 hireDate DATE, -- Date Employee Hired 5 orig_salary Number(8,2), -- Orignal Salary 6 curr_salary Number(8,2), -- Current Salary 7 region VARCHAR2(1 BYTE) -- Region where employeed 8 ) 9 /
Table created. SQL> SQL> SQL> -- prepare data for employee table SQL> insert into Employee(empno, ename, hireDate, orig_salary, curr_salary, region)
2 values(122,"Alison",to_date("19960321","YYYYMMDD"), 45000, 48000, "E") 3 /
1 row created. SQL> insert into Employee(empno, ename, hireDate, orig_salary, curr_salary, region)
2 values(123, "James",to_date("19781212","YYYYMMDD"), 23000, 32000, "W") 3 /
1 row created. SQL> insert into Employee(empno, ename, hireDate, orig_salary, curr_salary, region)
2 values(104,"Celia",to_date("19821024","YYYYMMDD"), 53000, 58000, "E") 3 /
1 row created. SQL> insert into Employee(empno, ename, hireDate, orig_salary, curr_salary, region)
2 values(105,"Robert",to_date("19840115","YYYYMMDD"), 31000, 36000, "W") 3 /
1 row created. SQL> insert into Employee(empno, ename, hireDate, orig_salary, curr_salary, region)
2 values(116,"Linda", to_date("19870730","YYYYMMDD"), 43000, 53000, "E") 3 /
1 row created. SQL> insert into Employee(empno, ename, hireDate, orig_salary, curr_salary, region)
2 values(117,"David", to_date("19901231","YYYYMMDD"), 78000, 85000, "W") 3 /
1 row created. SQL> insert into Employee(empno, ename, hireDate, orig_salary, curr_salary, region)
2 values(108,"Jode", to_date("19960917","YYYYMMDD"), 21000, 29000, "E") 3 /
1 row created. SQL> SQL> SQL> select * from employee;
EMPNO ENAME HIREDATE ORIG_SALARY CURR_SALARY R
---------- --------- ----------- ----------- -
122 Alison 21-MAR-96 45000 48000 E 123 James 12-DEC-78 23000 32000 W 104 Celia 24-OCT-82 53000 58000 E 105 Robert 15-JAN-84 31000 36000 W 116 Linda 30-JUL-87 43000 53000 E 117 David 31-DEC-90 78000 85000 W 108 Jode 17-SEP-96 21000 29000 E
7 rows selected. SQL> SQL> -- RATIO_TO_REPORT can easily be partioned as well SQL> SQL> SELECT ename, curr_salary, region,
2 curr_salary/SUM(curr_salary) OVER(PARTITION BY Region) 3 Portion, 4 RATIO_TO_REPORT(curr_salary) OVER(PARTITION BY Region) 5 Portion2 6 FROM employee 7 ORDER BY region, curr_salary;
ENAME CURR_SALARY R PORTION PORTION2
----------- - -------- --------
Jode 29000 E .1543 .1543 Alison 48000 E .2553 .2553 Linda 53000 E .2819 .2819 Celia 58000 E .3085 .3085 James 32000 W .2092 .2092 Robert 36000 W .2353 .2353 David 85000 W .5556 .5556 7 rows selected. SQL> SQL> SQL> SQL> -- clean the table SQL> drop table Employee
2 /
Table dropped. SQL> SQL> SQL>
</source>
RATIO_TO_REPORT(): compute the ratio of a value to the sum of a set of values
<source lang="sql">
SQL> CREATE TABLE all_sales (
2 year INTEGER, 3 month INTEGER, 4 prd_type_id INTEGER, 5 emp_id INTEGER , 6 amount NUMBER(8, 2) 7 );
Table created. SQL> SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
2 values(2006,1 ,1 ,21 ,16034.84);
1 row created. SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
2 values(2006,2 ,1 ,21 ,15644.65);
1 row created. SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
2 values(2006,3 ,2 ,21 ,20167.83);
1 row created. SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
2 values(2006,4 ,2 ,21 ,25056.45);
1 row created. SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
2 values(2006,5 ,2 ,21 ,NULL);
1 row created. SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
2 values(2006,6 ,1 ,21 ,15564.66);
1 row created. SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
2 values(2006,7 ,1 ,21 ,15644.65);
1 row created. SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
2 values(2006,8 ,1 ,21 ,16434.82);
1 row created. SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
2 values(2006,9 ,1 ,21 ,19654.57);
1 row created. SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
2 values(2006,10 ,1 ,21 ,21764.19);
1 row created. SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
2 values(2006,11 ,1 ,21 ,13026.73);
1 row created. SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
2 values(2006,12 ,2 ,21 ,10034.64);
1 row created. SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
2 values(2005,1 ,2 ,22 ,16634.84);
1 row created. SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
2 values(2005,1 ,2 ,21 ,26034.84);
1 row created. SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
2 values(2005,2 ,1 ,21 ,12644.65);
1 row created. SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
2 values(2005,3 ,1 ,21 ,NULL);
1 row created. SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
2 values(2005,4 ,1 ,21 ,25026.45);
1 row created. SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
2 values(2005,5 ,1 ,21 ,17212.66);
1 row created. SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
2 values(2005,6 ,1 ,21 ,15564.26);
1 row created. SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
2 values(2005,7 ,2 ,21 ,62654.82);
1 row created. SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
2 values(2005,8 ,2 ,21 ,26434.82);
1 row created. SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
2 values(2005,9 ,2 ,21 ,15644.65);
1 row created. SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
2 values(2005,10 ,2 ,21 ,21264.19);
1 row created. SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
2 values(2005,11 ,1 ,21 ,13026.73);
1 row created. SQL> insert into all_sales (YEAR,MONTH,PRD_TYPE_ID,EMP_ID,AMOUNT)
2 values(2005,12 ,1 ,21 ,10032.64);
1 row created. SQL> SQL> select * from all_sales;
YEAR MONTH PRD_TYPE_ID EMP_ID AMOUNT
---------- ----------- ---------- ----------
2006 1 1 21 16034.84 2006 2 1 21 15644.65 2006 3 2 21 20167.83 2006 4 2 21 25056.45 2006 5 2 21 2006 6 1 21 15564.66 2006 7 1 21 15644.65 2006 8 1 21 16434.82 2006 9 1 21 19654.57 2006 10 1 21 21764.19 2006 11 1 21 13026.73 2006 12 2 21 10034.64 2005 1 2 22 16634.84 2005 1 2 21 26034.84 2005 2 1 21 12644.65 2005 3 1 21 2005 4 1 21 25026.45 2005 5 1 21 17212.66 2005 6 1 21 15564.26 2005 7 2 21 62654.82 2005 8 2 21 26434.82 2005 9 2 21 15644.65 2005 10 2 21 21264.19 2005 11 1 21 13026.73 2005 12 1 21 10032.64
25 rows selected. SQL> SQL> --RATIO_TO_REPORT(): compute the ratio of a value to the sum of a set of values. SQL> SQL> SQL> SQL> SELECT
2 month, prd_type_id, 3 SUM(amount) AS prd_type_amount, 4 RATIO_TO_REPORT(SUM(amount)) OVER (PARTITION BY month) 5 AS prd_type_ratio 6 FROM all_sales 7 WHERE month <= 3 8 GROUP BY month, prd_type_id 9 ORDER BY month, prd_type_id; MONTH PRD_TYPE_ID PRD_TYPE_AMOUNT PRD_TYPE_RATIO
----------- --------------- --------------
1 1 16034.84 .273144896 1 2 42669.68 .726855104 2 1 28289.3 1 3 1 3 2 20167.83 1
SQL> SQL> SQL> SQL> drop table all_sales; Table dropped. SQL> SQL>
</source>
RATIO_TO_REPORT Demo
<source lang="sql">
SQL> SQL> -- create demo table SQL> create table Employee(
2 empno Number(3) NOT NULL, -- Employee ID 3 ename VARCHAR2(10 BYTE), -- Employee Name 4 hireDate DATE, -- Date Employee Hired 5 orig_salary Number(8,2), -- Orignal Salary 6 curr_salary Number(8,2), -- Current Salary 7 region VARCHAR2(1 BYTE) -- Region where employeed 8 ) 9 /
Table created. SQL> SQL> SQL> -- prepare data for employee table SQL> insert into Employee(empno, ename, hireDate, orig_salary, curr_salary, region)
2 values(122,"Alison",to_date("19960321","YYYYMMDD"), 45000, 48000, "E") 3 /
1 row created. SQL> insert into Employee(empno, ename, hireDate, orig_salary, curr_salary, region)
2 values(123, "James",to_date("19781212","YYYYMMDD"), 23000, 32000, "W") 3 /
1 row created. SQL> insert into Employee(empno, ename, hireDate, orig_salary, curr_salary, region)
2 values(104,"Celia",to_date("19821024","YYYYMMDD"), 53000, 58000, "E") 3 /
1 row created. SQL> insert into Employee(empno, ename, hireDate, orig_salary, curr_salary, region)
2 values(105,"Robert",to_date("19840115","YYYYMMDD"), 31000, 36000, "W") 3 /
1 row created. SQL> insert into Employee(empno, ename, hireDate, orig_salary, curr_salary, region)
2 values(116,"Linda", to_date("19870730","YYYYMMDD"), 43000, 53000, "E") 3 /
1 row created. SQL> insert into Employee(empno, ename, hireDate, orig_salary, curr_salary, region)
2 values(117,"David", to_date("19901231","YYYYMMDD"), 78000, 85000, "W") 3 /
1 row created. SQL> insert into Employee(empno, ename, hireDate, orig_salary, curr_salary, region)
2 values(108,"Jode", to_date("19960917","YYYYMMDD"), 21000, 29000, "E") 3 /
1 row created. SQL> SQL> SQL> select * from employee;
EMPNO ENAME HIREDATE ORIG_SALARY CURR_SALARY R
---------- --------- ----------- ----------- -
122 Alison 21-MAR-96 45000 48000 E 123 James 12-DEC-78 23000 32000 W 104 Celia 24-OCT-82 53000 58000 E 105 Robert 15-JAN-84 31000 36000 W 116 Linda 30-JUL-87 43000 53000 E 117 David 31-DEC-90 78000 85000 W 108 Jode 17-SEP-96 21000 29000 E
7 rows selected. SQL> SQL> -- RATIO_TO_REPORT SQL> SQL> COLUMN portion2 LIKE portion SQL> SELECT ename, curr_salary,
2 curr_salary/SUM(curr_salary) OVER() Portion, 3 RATIO_TO_REPORT(curr_salary) OVER() Portion2 4 FROM employee 5 ORDER BY curr_salary;
ENAME CURR_SALARY PORTION PORTION2
----------- -------- --------
Jode 29000 .0850 .0850 James 32000 .0938 .0938 Robert 36000 .1056 .1056 Alison 48000 .1408 .1408 Linda 53000 .1554 .1554 Celia 58000 .1701 .1701 David 85000 .2493 .2493 7 rows selected. SQL> SQL> SQL> -- clean the table SQL> drop table Employee
2 /
Table dropped. SQL> SQL> SQL>
</source>