 |
|
Oracle SQL "partition" Analytic Function tips
Oracle Tips by Laurent Schneider
|
Laurent
Schneider is considered one of the top Oracle SQL experts, and
he is the author of the book "Advanced
Oracle SQL Programming" by Rampant TechPress. The following
is an excerpt from the book.
PARTITION
As the aggregate functions use GROUP BY to group rows,
analytic functions use PARTITION BY.
SELECT
ENAME,
SAL,
DEPTNO,
MAX(SAL) OVER (PARTITION BY DEPTNO) MAX_SAL_DEPTNO,
JOB,
MAX(SAL) OVER (PARTITION BY JOB) MAX_SAL_JOB
FROM
EMP;
ENAME SAL DEPTNO
MAX_SAL_DEPTNO JOB MAX_SAL_JOB
---------- ---------- ---------- -------------- --------- -----------
MILLER 1300 10 5000 CLERK 1300
KING 5000 10 5000 PRESIDENT 5000
CLARK 2450 10 5000 MANAGER 2975
SMITH 800 20 3000 CLERK 1300
SCOTT 3000 20 3000 ANALYST 3000
ADAMS 1100 20 3000 CLERK 1300
FORD 3000 20 3000 ANALYST 3000
JONES 2975 20 3000 MANAGER 2975
WARD 1250 30 2850 SALESMAN 1600
MARTIN 1250 30 2850 SALESMAN 1600
TURNER 1500 30 2850 SALESMAN 1600
ALLEN 1600 30 2850 SALESMAN 1600
JAMES 950 30 2850 CLERK 1300
BLAKE 2850 30 2850 MANAGER 2975
Miller is the clerk of department 10. In his department, the
best paid employee has a salary of 5000. Over all clerks, his salary of 1300 is
the highest. The PARTITION BY reduces the scope to the current partition.