PL/SQL Analytic functions

Oracle PL/SQL analytical functions are a powerful feature that allows you to perform complex analyses and calculations across rows related to a specific query result set. These functions operate on a set of rows related to the current row and provide a way to perform calculations without the need for self-joins or subqueries. Analytical functions are typically used in conjunction with the SELECT statement and the OVER() clause to define the window of rows for the analytical computation.

Here are some key aspects of analytical functions in Oracle:

Windowing Clause:

Analytical functions use a windowing clause to define the set of rows to which the function is applied. This window is based on the ordering of rows specified in the ORDER BY clause. The window can be defined in terms of physical offset or a logical range.

Partitioning:

Analytical functions support partitioning, which allows you to divide the result set into partitions to perform calculations separately within each partition. This is useful when you want to compute values independently for different groups within your data.

Elimination of Self-Joins:

Analytical functions eliminate the need for self-joins or subqueries, making queries more readable and efficient. They allow you to access related rows without explicitly joining the table to itself, simplifying the SQL code.

Ranking and Row Numbering:

Analytical functions are commonly used for ranking and numbering rows based on a specified criteria. This is useful in scenarios where you need to identify the top N rows, find the rank of a particular value, or assign unique row numbers to each row.

Moving Aggregates:

Analytical functions enable the calculation of moving aggregates, such as cumulative sums or averages. This is valuable when you need to track the running total or average of a particular column across a specified order.

Performance Optimization:

Analytical functions are often more efficient than equivalent self-joins or subqueries. The database engine is optimized to handle analytical functions, resulting in faster query execution.

Simplified SQL Queries:

Analytical functions contribute to more concise and readable SQL queries. The code is often simpler and easier to understand, leading to improved maintainability.

With Oracle PL/SQL analytic functions, you can calculate rank, density, cumulative distribution, and more.
In this article, we will take a look at the syntax and examples of some of the most popular Oracle PL/SQL Analytic functions.

ROW_NUMBER assigns a unique number to each row within a result set, based on the order specified in the ORDER BY clause.

SELECT ROW_NUMBER() OVER (ORDER BY salary DESC) as "Rank", last_name, salary 
FROM employees;

RANK assigns a rank to each row within a result set, based on the order specified in the ORDER BY clause. If two or more rows have the same value, they will receive the same rank.

SELECT RANK() OVER (ORDER BY salary DESC) as "Rank", last_name, salary
FROM employees;

DENSE_RANK assigns a rank to each row within a result set, based on the order specified in the ORDER BY clause. If two or more rows have the same value, the next rank will not be skipped.

SELECT DENSE_RANK() OVER (ORDER BY salary DESC) as "Rank", last_name, salary
FROM employees;

LISTAGG this function aggregates a list of values into a single value.

SELECT LISTAGG(last_name, '; ') WITHIN GROUP (ORDER BY last_name)
FROM employees;

PERCENT_RANK calculates the percentage rank of a row within a group of rows.

SELECT PERCENT_RANK(4500, .05) WITHIN GROUP (ORDER BY salary, comm) "Percent-Rank" 
FROM employees;

CUME_DIST calculates the cumulative distribution of a row within a group of rows.

SELECT CUME_DIST(4500, .05) WITHIN GROUP (ORDER BY salary, comm) "Cume-Dist" 
FROM employees;

NTILE divides the result set into a specified number of groups or “tiles.”

SELECT NTILE(3) OVER (ORDER BY salary DESC) as "Quartile", last_name, salary
FROM employees;

LAD and LEAD: accesses data from a previous or next row in the same result set

SELECT last_name, salary, LAG(salary, 1) OVER (ORDER BY last_name) as "Previous Salary"
FROM employees;

These are just a few examples of the many analytic functions available in PL/SQL. Each function can be customized and combined with others to suit specific needs and provide more detailed analysis of your data.

In conclusion, analytical functions in Oracle Database provide a powerful and efficient way to perform complex calculations and analyses within SQL queries. They offer a concise and readable syntax, eliminate the need for self-joins, and provide flexibility in partitioning and windowing, making them a valuable tool for data analysis and reporting. When dealing with tasks such as ranking, windowed aggregates, or row numbering, leveraging analytical functions can lead to more efficient and maintainable code.