Professional Documents
Culture Documents
When? – Starting 8i
. Top-N Queries
SELECT deptno,
DEPTNO SAL
sum(sal) sal
-------------------------
FROM emp
30 4100
GROUP BY deptno
20 3775
DEPTNO SAL
SELECT deptno,
--------------------------
sum(sal) over() sal
20 7875
FROM emp
30 7875
30 7875
20 7875
30 7875
How are analytic functions different from group or aggregate functions?
DEPTNO DEPT_COUNT
SELECT deptno, --------------- --------------------
COUNT(*) DEPT_COUNT 20 2
FROM emp 30 3
WHERE deptno IN (20, 30) 2 rows selected
GROUP BY deptno;
SELECT empno ,
deptno,
COUNT(*) OVER (PARTITION BY deptno) DEPT_COUNT
FROM emp
WHERE deptno IN (20, 30); EMPNO DEPTNO DEPT_COUNT
----------- ---------- ----------
7369 20 2
7566 20 2
7499 30 3
7900 30 3
7844 30 3
5 rows selected.
SELECT ename,
deptno,
sal,
sum(sal) over () Tot,
sum(sal) over (order by deptno,ename) Run_Tot,
sum(sal) over (partition by deptno order by ename) Dept_Tot,
row_number() over (partition by deptno order by ename) Seq
FROM emp ORDER BY deptno,ename ;
ENAME DEPTNO SAL TOT RUN_TOT DEPT_TOT SEQ
Analytic functions are computed after all joins, WHERE clause, GROUP
BY and HAVING are computed on the query.
The main ORDER BY clause of the query operates after the analytic
functions.
So analytic functions can only appear in the select list and in the main
ORDER BY clause of the query.
ROW_NUMBER()
LAG()
LEAD()
MIN()
MAX()
RANK()
DENSE_RANK()
SUM()
AVG()
FIRST_VALUE()
LAST_VALUE()
FIRST()
LAST()
ROW_NUMBER
FUNCTION
ROW_NUMBER( ) gives a running serial number to a partition of
records. It is very useful in reporting, especially in places where
different partitions have their own serial numbers.
SELECT empno,
deptno,
hiredate,
ROW_NUMBER() OVER (PARTITION BY
deptno ORDER BY hiredate
NULLS LAST) SRLNO
FROM emp EMPNO DEPTNO HIREDATE SRLNO
WHERE deptno IN (10,20)
7782 10 09-JUN-81 1
OREDR BY deptno,SRLNO
7839 10 17-NOV-81 2
7934 10 23-JAN-82 3
7369 20 17-DEC-80 1
7566 20 02-APR-81 2
7902 20 03-DEC-81 3
7788 20 09-DEC-82 4
7876 20 12-JAN-83 5
RANK
DENSE_RANK FUNCTIONS
(or)
2.RANGE Windows
Syntax:
Function( ) OVER (PARTITION BY <expr1> ORDER BY
<expr2> RANGE BETWEEN <start_expr> AND <end_expr>)
(or)
SELECT ename,
sal,
hiredate,
hiredate-50 "50_days_prior",
first_value(ename) over
(order by hiredate asc range 50
preceding) first_ename,
first_value(hiredate) over (order
by hiredate asc range 50 preceding)
first_hdate
FROM emp ORDER BY hiredate ASC
ENAME SAL HIREDATE 50_days_p FIRST_ENAM FIRST_HDA
----------------------------------------------------------------------------------------------------
SMITH 800 17-DEC-80 28-OCT-80 SMITH 17-DEC-80
ALLEN 1600 20-FEB-81 01-JAN-81 ALLEN 20-FEB-81
WARD 1250 22-FEB-81 03-JAN-81 ALLEN 20-FEB-81
JONES 2975 02-APR-81 11-FEB-81 ALLEN 20-FEB-81
BLAKE 2850 01-MAY-81 12-MAR-81 JONES 02-APR-81
CLARK 2450 09-JUN-81 20-APR-81 BLAKE 01-MAY-81
TURNER 1500 08-SEP-81 20-JUL-81 TURNER 08-SEP-81
MARTIN 1250 28-SEP-81 09-AUG-81 TURNER 08-SEP-81
KING 5000 17-NOV-81 28-SEP-81 MARTIN 28-SEP-81
FORD 3000 03-DEC-81 14-OCT-81 KING 17-NOV-81
JAMES 950 03-DEC-81 14-OCT-81 KING 17-NOV-81
MILLER 1300 23-JAN-82 04-DEC-81 MILLER 23-JAN-82
SCOTT 3000 19-APR-87 28-FEB-87 SCOTT 19-APR-87
ADAMS 1100 23-MAY-87 03-APR-87 SCOTT 19-APR-87
Process Resultset Using Minimal Resources
Easier To Code
Oracle calls the concept of filling in missing data with partitioned outer
joins Data Densification.
LEFT OUTER JOIN,PARTITIONED BY ,KEEP are some new features in
10g