Vous êtes sur la page 1sur 2

Oracle Analytic Functions

Written by Saurav Mitra - Last Updated Friday, 30 April 2010 12:59

Oracle Analytic Functions compute an aggregate value based on a group of rows. It


opens up a whole new way of looking at the data. This article explains how we can unleash the
full potential of this.

Analytic functions differ from aggregate functions in the sense that they return multiple rows for
each group. The group of rows is called a window and is defined by the analytic clause. For
each row, a sliding window of rows is defined. The window determines the range of rows used
to perform the calculations for the current row.

Oracle provides many Analytic Functions such as


AVG, CORR, COVAR_POP, COVAR_SAMP, COUNT, CUME_DIST, DENSE_RANK, FIRST,
FIRST_VALUE, LAG, LAST, LAST_VALUE, LEAD, MAX, MIN, NTILE, PERCENT_RANK,
PERCENTILE_CONT, PERCENTILE_DISC, RANK, RATIO_TO_REPORT, STDDEV,
STDDEV_POP, STDDEV_SAMP, SUM, VAR_POP, VAR_SAMP, VARIANCE.

The Syntax of analytic functions: Analytic-Function(Column1,Column2,...) OVER (


[Query-Partition-Clause] [Order-By-Clause] [Windowing-Clause] )

Analytic functions take 0 to 3 arguments.

An Example:
SELECT ename, deptno, sal,
SUM(sal)
OVER (ORDER BY deptno, ename) AS Running_Total,
SUM(sal)
OVER ( PARTITION BY deptno
ORDER BY ename) AS Dept_Total,
ROW_NUMBER()
OVER (PARTITION BY deptno
ORDER BY ename) As Sequence_No
FROM emp
ORDER BY deptno, ename;

1/2
Oracle Analytic Functions

Written by Saurav Mitra - Last Updated Friday, 30 April 2010 12:59

of
ENAME
the
2.The
1. other
partition
clause
set groups.
by thesorts
clause
they
Order-By-Clause
definitely
ORDER
affect BYthe The
Query-Partition-Clause
independently,
criteria
The PARTITION the
partition
BY are
clause makes
SUM(sal)
data
clause
output reset within
the
expressions.
logically
afor
specifies
of the is
SUM(sal)
each each
'reset'department
breaks
group.
The asbe
athe
computed
department
analytic
single by ENAME;
functions
result within
setchanges.
are each
into department,
applied
N The
groups, ORDER
tothe
each independent
BY
according
group towhich
the
the3. Windowing-Clause
function
The
continues
analytic
default
currentwindowing
Let's
SELECT compute
to
row's
look
SUM(sal)
FROMOVER
ORDER window
function
the
(an current
clause
salary
deptno,
ORDER
ROWS
emp
BY its
example
PARTITION
2BY
deptno, is
will
value
angives
column
ename,
ename
PRECEDING row.
operate,
anchored
based
with
plus
sal,
BY
ename; us )analytic
a how
within
on
way
window
sliding
the
deptno
AS any the
to
a function.
window
previousdata is
arbitrary
group.
define
that
within
Sliding_Total sorted
2simply
This
arows
sliding
sliding within
aclause
starts
inor
or
group
that and each
anchored
anchored
can
at the
be group
first
used
window
window
compute
group. i.erow
to
ROW (partition).
of
have
within
a
ofgroup
sumdata,
the This
aanalytic
of
Window group.
on
an
the will
clause:

of Now
two preceding
ifFROM
we *look rows
at the inSliding
the window.
Total value
[800+3000+2975
of SMITH willitadd
is
=
to6775]
simply SMITH's . Itsalary plus the salary
)We
from
existance
UNBOUNDED
specified
**
Suppose
SELECT
)OVER
salary
for can
the by
Solving
PARTITION
Rnk
This
each
for
will
SELECT
SELECT
)OVER set
of
we
dept.
PARTITION
Rnk
WHERE FROM the
an up
((current
Top-N
want
deptno,
each
give BYwindows
row
PRECEDING.
ORDER
ORDER
emp;
FROM
deptno,
Rnk BY
emp to
ename,
deptno
department
us theename,
deptno BY
BY
Queries
(find based
in
out **
sal,
ORDER
employee
or an
the
sal,
ORDER on
clause.
Thatname two
analytic
says
top criteria:
to
3 andfunction
get
salaried
ROW_NUMBER()
BY sal DESC
partition/group
ROW_NUMBER()
BY sal salary
DESC .RANGES
allemployee
rows
Now
with in
ranksaofdefault
our
of
get data
each
based
the values
partitionwindow
3that
department:
top on highestorclause
came
can
descending ROWS
be
before
said,
paid of offset
RANGE
orderus
that
ofasthe
employees

2/2

Vous aimerez peut-être aussi