ORacle SQL Analytics
ORacle SQL Analytics
SQL Analytics
Lewis R Cunningham Database Architect Sheperd Systems An Expert's Guide to Oracle http://blogs.ittoolbox.com/oracle/guide
An expert is a person who has made all the mistakes that can be made in a very narrow field. - Niels Bohr (1885 1962)
3 05/25/06
Introduction
Analytic functions were introduced in Release 2 of 8i and simplify greatly the means by which pivot reports and OLAP queries can be computed in straight, non-procedural SQL. Prior to the introduction of analytic functions, complex reports could be produced in SQL by complex self-joins, sub-queries and inline-views but these were resource-intensive and very inefficient.
4 September 26, 2005
Introduction
Furthermore, if a question to be answered was too complex, it could be written in PL/SQL, which by its very nature is usually less efficient than a single SQL statement
The Syntax
The Syntax
Query-Partition-Clause -Logically breaks a single result set into N groups, according to the criteria set by the partition expressions. The words "partition" and "group" are used synonymously here. The analytic functions are applied to each group independently, they are reset for each group Order-By-Clause - Specifies how the data is sorted within each group (partition). This will definitely affect the outcome of any analytic function.
9 September 26, 2005
The Syntax
Windowing-Clause - The windowing clause gives us a way to define a sliding or anchored window of data, on which the analytic function will operate, within a group. This clause can be used to have the analytic function compute its value based on any arbitrary sliding or anchored window within a group.
10 September 26, 2005
ROW_NUMBER function
ROW_NUMBER is an analytic function. It assigns a unique number to each row to which it is applied (either each row in the partition or each row returned by the query), in the ordered sequence of rows specified in the order_by_clause, beginning with 1.
LAST_NAME Ozer Errazuriz Partners Russell Cambrault Hunold Ernst Austin Pataballa Lorentz
DENSE_RANK function
DENSE_RANK computes the rank of a row in an ordered group of rows. The ranks are consecutive integers beginning with 1. The largest rank value is the number of unique values returned by the query. Rank values are not skipped in the event of ties. Rows with equal values for the ranking criteria receive the same rank.
18 September 26, 2005
RANK function
RANK calculates the rank of a value in a group of values. Rows with equal values for the ranking criteria receive the same rank. Oracle then adds the number of tied rows to the tied rank to calculate the next rank. Therefore, the ranks may not be consecutive numbers.
The FIRST_VALUE and LAST_VALUE functions allow you to select the first and last rows from a group. These rows are especially valuable because they are often used as the baselines in calculations.
Summary
Analytic functions provide an easy mechanism to compute resultsets that, before 8i, were inefficient, impractical and, in some cases, impossible in "straight SQL". In addition to their flexibility and power, they are also extremely efficient.
Conclusion
This new set of functionality holds some exciting possibilities. It opens up a whole new way of looking at the data. It will remove a lot of procedural code and complex or inefficient queries that would have taken a long time to develop. Add analytic functions to your SQL arsenal and actively seek opportunities to use them.
SQL Analytics
Lewis R Cunningham Database Architect Sheperd Systems An Expert's Guide to Oracle http://blogs.ittoolbox.com/oracle/guide
An expert is a person who has made all the mistakes that can be made in a very narrow field. - Niels Bohr (1885 1962)