Resumen Desde la versión 8i, Oracle proporciona una serie de funciones que nos pueden facilitar bastante los informes analíticos: rankings, acumulados, porcentajes, etc. select a.deptno dp, a.ename, a.sal, b.dept_max_sal
from scott.emp a, (select deptno, max(sal) dept_max_sal from scott.emp group by deptno) b where a.deptno=b.deptno; DP ENAME SAL DEPT_MAX_SAL ---------- ---------- ---------- ------------ 10 CLARK 2450 5000 10 KING 5000 5000 10 MILLER 1300 5000 20 SMITH 800 3000 20 ADAMS 1100 3000 20 FORD 3000 3000 20 SCOTT 3000 3000 20 JONES 2975 3000 30 ALLEN 1760 2850 30 BLAKE 2850 2850 30 MARTIN 1375 2850 30 JAMES 950 2850 30 TURNER 1500 2850 30 WARD 1375 2850 14 rows selected. select a.deptno dp, a.ename, a.sal,
max(sal) over (partition by deptno) dept_max_sal from scott.emp a; DP ENAME SAL DEPT_MAX_SAL ---------- ---------- ---------- ------------ 10 CLARK 2450 5000 10 KING 5000 5000 10 MILLER 1300 5000 20 SMITH 800 3000 20 ADAMS 1100 3000 20 FORD 3000 3000 20 SCOTT 3000 3000 20 JONES 2975 3000 30 ALLEN 1760 2850 30 BLAKE 2850 2850 30 MARTIN 1375 2850 30 JAMES 950 2850 30 TURNER 1500 2850 30 WARD 1375 2850 14 rows selected. La función que hemos usado se compone de dos partes: select deptno dp,ename, job, sal,
max(sal) over (partition by deptno) dept_max_sal, max(sal) over (partition by job) job_max_sal from scott.emp; DP ENAME JOB SAL DEPT_MAX_SAL JOB_MAX_SAL ---------- ---------- --------- ---------- ------------ ----------- 10 MILLER CLERK 1300 5000 1300 10 CLARK MANAGER 2450 5000 2975 10 KING PRESIDENT 5000 5000 5000 20 SCOTT ANALYST 3000 3000 3000 20 FORD ANALYST 3000 3000 3000 20 SMITH CLERK 800 3000 1300 20 JONES MANAGER 2975 3000 2975 20 ADAMS CLERK 1100 3000 1300 30 JAMES CLERK 950 2850 1300 30 MARTIN SALESMAN 1375 2850 1760 30 TURNER SALESMAN 1500 2850 1760 30 WARD SALESMAN 1375 2850 1760 30 ALLEN SALESMAN 1760 2850 1760 30 BLAKE MANAGER 2850 2850 2975 14 rows selected. select deptno,ename,sal,top3
from (select deptno, ename, sal, dense_rank() over (partition by deptno order by sal desc) top3 from scott.emp) where top3<=3 DEPTNO ENAME SAL TOP3 ---------- ---------- ---------- ---------- 10 KING 5000 1 10 CLARK 2450 2 10 MILLER 1300 3 20 SCOTT 3000 1 20 FORD 3000 1 20 JONES 2975 2 20 ADAMS 1100 3 30 BLAKE 2850 1 30 ALLEN 1760 2 30 TURNER 1500 3 10 rows selected. Analicemos la consulta anterior: select deptno,ename,sal,
(cume_dist() over (partition by deptno order by sal))*100 pct from scott.emp; DEPTNO ENAME SAL PCT ---------- ---------- ---------- ---------- 10 MILLER 1300 33.3333333 10 CLARK 2450 66.6666667 10 KING 5000 100 20 SMITH 800 20 20 ADAMS 1100 40 20 JONES 2975 60 20 SCOTT 3000 100 20 FORD 3000 100 30 JAMES 950 16.6666667 30 WARD 1375 50 30 MARTIN 1375 50 30 TURNER 1500 66.6666667 30 ALLEN 1760 83.3333333 30 BLAKE 2850 100 14 rows selected. select deptno dp, hiredate, sal,
sum(sal) over (order by hiredate ROWS UNBOUNDED PRECEDING) acum from scott.emp; DP HIREDATE SAL ACUM ---------- --------- ---------- ---------- 20 17-DEC-80 800 800 30 20-FEB-81 1760 2560 30 22-FEB-81 1375 3935 20 02-APR-81 2975 6910 30 01-MAY-81 2850 9760 10 09-JUN-81 2450 12210 30 08-SEP-81 1500 13710 30 28-SEP-81 1375 15085 10 17-NOV-81 5000 20085 30 03-DEC-81 950 21035 20 03-DEC-81 3000 24035 10 23-JAN-82 1300 25335 20 09-DEC-82 3000 28335 20 12-JAN-83 1100 29435 14 rows selected.
Última modificación ( 27.08.2010 )
|
|
||||||||||||||
|
|
|||||||||||||||




