Oracle Analytic Functions Session1
Oracle Analytic Functions Session1
Learning Series
Session 1
Introduction
Simple solution
Reinvent
General Syntax
Function(arg1,..., argn)
OVER ( [PARTITION BY <...>]
[ORDER BY <....>]
[<window_clause>]
)
DEPTNO DEPT_COUNT
---------------------- ----------------------
20 5
30 6
Different from the ORDER BY clause of the main query which comes
after WHERE
OVER (
PARTITION BY deptno
ORDER BY hiredate
NULLS LAST
)
Today We are Discussing..
Picks the first record from the partition after doing the ORDER BY
How many days after the first hire of each department were the next employees hired?
SELECT empno, deptno,
hiredate - FIRST_VALUE(hiredate)
OVER ( PARTITION BY deptno
ORDER BY hiredate ) DAY_GAP
FROM emp
WHERE deptno IN (20, 30)
ORDER BY deptno, DAY_GAP;
EMPNO DEPTNO DAY_GAP
---------- ---------- ----------
7369 20 0
7566 20 106
7902 20 351
7788 20 722
7876 20 756
7499 30 0
7521 30 2
7698 30 70
7844 30 200
7654 30 220
7900 30 286
FIRST and LAST function