100% found this document useful (1 vote)
121 views

Analytic Functions

The document discusses analytic functions in Oracle SQL. It describes the syntax of analytic functions which include an analytic clause with optional query partition, order by, and windowing clauses. It provides examples of using analytic functions like AVG, COUNT, DENSE_RANK, and LAG to calculate rolling averages, counts, ranks, and access prior rows over partitions of data.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
121 views

Analytic Functions

The document discusses analytic functions in Oracle SQL. It describes the syntax of analytic functions which include an analytic clause with optional query partition, order by, and windowing clauses. It provides examples of using analytic functions like AVG, COUNT, DENSE_RANK, and LAG to calculate rolling averages, counts, ranks, and access prior rows over partitions of data.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Analytic Functions

select empno,deptno,sal,avg(sal) over (PARTITION BY deptno) as


avg_dept_no
from emp
Analytic Function Syntax
analytic_function([ arguments ]) OVER (analytic_clause)
The analytic_clause breaks down into the following optional elements.

[ query_partition_clause ] [ order_by_clause [ windowing_clause ] ]


query_partition_clause
The query_partition_clause divides the result set into partitions, or groups, of data. The
operation of the analytic function is restricted to the boundary imposed by these partitions,
similar to the way a GROUP BY clause affects the action of an aggregate function. If
the query_partition_clause is omitted, the whole result set is treated as a single partition.
The following query uses an empty OVER clause, so the average presented is based on all the
rows of the result set.

windowing_clause
We have seen previously the query_partition_clause controls the window, or group of
rows, the analytic operates on. The windowing_clause gives some analytic functions a further
degree of control over this window within the current partition, or whole result set if no
partitioning clause is used. The windowing_clause is an extension of
the order_by_clause and as such, it can only be used if an order_by_clause is present.
The windowing_clause has two basic forms.

RANGE BETWEEN start_point AND end_point


ROWS BETWEEN start_point AND end_point
Possible values for "start_point" and "end_point" are:

 UNBOUNDED PRECEDING : The window starts at the first row of the partition, or the
whole result set if no partitioning clause is used. Only available for start points.
 UNBOUNDED FOLLOWING : The window ends at the last row of the partition, or the whole
result set if no partitioning clause is used. Only available for end points.
 CURRENT ROW : The window starts or ends at the current row. Can be used as start or
end point.
 value_expr PRECEDING : A physical or logical offset before the current row using a
constant or expression that evaluates to a positive numerical value. When used
with RANGE, it can also be an interval literal if the order_by_clause uses
a DATE column.
 value_expr FOLLOWING : As above, but an offset after the current row.

SELECT empno, deptno, sal,


AVG(sal) OVER (PARTITION BY deptno ORDER BY sal
RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT
ROW) AS range_avg,
AVG(sal) OVER (PARTITION BY deptno ORDER BY sal
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
AS rows_avg
FROM emp;

EMPNO DEPTNO SAL RANGE_AVG ROWS_AVG


---------- ---------- ---------- ---------- ----------
7934 10 1300 1300 1300
7782 10 2450 1875 1875
7839 10 5000 2916.66667 2916.66667

7369 20 800 800 800


7876 20 1100 950 950
7566 20 2975 1625 1625
7788 20 3000 2175 1968.75
7902 20 3000 2175 2175

7900 30 950 950 950


7654 30 1250 1150 1100
7521 30 1250 1150 1150
7844 30 1500 1237.5 1237.5
7499 30 1600 1310 1310
7698 30 2850 1566.66667 1566.66667
As we can see running total are correctly calculated when using ROWS BETWEEN UNBOUNDED
PRECEDING AND CURRENT ROW in case values are duplicate.

in my opinion, the default windowing_clause should have been ROWS BETWEEN UNBOUNDED


PRECEDING AND UNBOUNDED FOLLOWING. This would make the accidental inclusion of
the windowing_clause much less confusing.

With expression

Count the number ordered created on same date for same customer.

COUNT(order_id) over ( PARTITION by customer_id ORDER BY


TRUNC(creation_date)
RANGE BETWEEN INTERVAL '1' DAY PRECEDING AND
INTERVAL '1' DAY FOLLOWING
)
Oracle DENSE_RANK
Unlike the RANK() function, the DENSE_RANK() function returns rank values as consecutive
integers. It does not skip rank in case of ties. Rows with the same values for the rank criteria will
receive the same rank values.

DENSE_RANK( ) OVER([ query_partition_clause ] order_by_clause)


In this syntax, the order_by_clause is required because the DENSE_RANK() function is
ordered sensitive. The following is the syntax of the order by clause:

ORDER BY expression1 [,expression2,...] [ASC | DESC ] [NULLS FIRST |


LAST]

Oracle LAG
in this tutorial, you will learn how to access the row at a given offset prior to the current row
using Oracle LAG() function.

Oracle LAG() is an analytic function that allows you to access the row at a given offset prior to
the current row without using a self-join.

The following illustrates the syntax of the LAG() function:


LAG(expression [, offset ] [, default ]) OVER
( [ query_partition_clause ] order_by_clause )

Code language: SQL (Structured Query Language) (sql)

In this syntax:

You might also like