Analytic Functions in Oracle
Analytic Functions in Oracle
By,
Ravi.G
Analytic functions compute an aggregate value based on a group of rows.
They differ from aggregate functions in 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. Window sizes can be
based on either a physical number of rows or a logical interval such as time.
Analytic functions are the last set of operations performed in a query except for the final ORDER BY clause. All joins and
all WHERE, GROUP BY, and HAVING clauses are completed before the analytic functions are processed. Therefore,
analytic functions can appear only in the select list or ORDER BY clause.
Analytic functions are commonly used to compute cumulative, moving, centered, and reporting aggregates.
Easy to use
Efficient: removes a lot of procedural code and complex or inefficient queries that would have taken a long tome to
develop, to achieve the same result
Difference between Analytic and SQL Functions
Difference 1: For SQL functions, non-”group by” column is not allowed in the select clause
• The following query will not work:
select dept,name, count(1) from emp where dept in ('128','130') group by dept order by dept
• Analytic function allows the non-"group by" column to present in select clause
select dept,name, count(1) over(partition by dept) empNum from emp where dept in ('128','130') order
by dept
• Analytic functions calculate the aggregation without grouping rows
Difference 2: Analytic function can only appear in the SELECT clause and in the main ORDER BY clause of a query,
because analytic functions are computed after all JOIN, WHERE, GROUP BY and HAVING are computed on the
query
-- correct query
select dept,count(1) from emp where dept in (166,168) group by dept having count(1)>20
-- wrong query
select dept,count(1) over() from emp where dept in (166,168) group by dept having count(1) over()>20
List of Analytic-Functions
• Argument: 0 -3 arguments
• Query-partition-clause: break result set into groups
• Order-by-clause: specifies how the data is sorted within each group
• Windowing-clause: define a sliding or anchored window of data within a group
select dept,count(*) over (partition by dept) from emp where dept between '100' and '300' order by dept;
1. SELECT *FROM EMP
2. SELECT DEPTNO,COUNT(*) FROM EMP GROUP BY DEPTNO
select distinct object_name from all_arguments where package_name = 'STANDARD' and object_name like
'CUBE';
Analytic functions for descriptive statistics include the following : Maximum, Minimum, Average, Median, Count
The functions SUM, COUNT, AVG, MIN, MAX are the common analytic functions the result of which does not
depend on the order of the records.
SUM() Function : MAX() Function :
SELECT DEPTNO,SUM(SAL) OVER (PARTITION BY SELECT DEPTNO,MAX(SAL) OVER (PARTITION BY DEPTNO)
DEPTNO) DEPT_SUM FROM EMP DEPT_MAX FROM EMP
SELECT EMPNO,DEPTNO,SAL,RANK() OVER (PARTITION BY DEPTNO ORDER BY SAL DESC) RANK,DENSE_RANK() OVER
(PARTITION BY DEPTNO ORDER BY SAL DESC) DENSE_RANK FROM EMP
LEAD and LAG
LEAD has the ability to compute an expression on the next rows (rows which are going to come after the current row)
Functions
and return the value to the current row. The general syntax of LEAD is shown below:
-- How each employee's salary compare with the average salary of the first -- year
hires of their department?
Defining the PARTITOIN BY and ORDER BY clauses on indexed columns (ordered in accordance with the
PARTITION CLAUSE and then the ORDER BY clause in analytic function) will provide optimum performance.
Even in absence of indexes analytic functions provide acceptable performance but need to do sorting for computing
partition and order by clause.
If the query contains multiple analytic functions, sorting and partitioning on two different columns should be avoided
if they are both not indexed.
C:\Documents and
Settings\ravi.g\Desktop
Queries
THANKS