This paper gives overview of
the most important analytical functions, that are often used in every day life
in DSS or DW business.
Analytic Function was first
introduced in Oracle 8i with some new functionality in further releases. Check
the links at the end for further examples and documentation.
This description is based on
Oracle 10g
Feature |
Description |
Example |
Set up |
Use this script to set up the environment. |
|
|
The script creates two table prod and month and run all the examples |
|
Data |
Check the complete data of the product table … |
|
|
and the time dimension table |
|
ROLLUP, CUBE |
Use rollup and cube to aggregate on more than one grouping set. Use
GROUPING to distinct between NULLs and additional generated rows |
|
|
Group by quarter and product, get subtotals .. |
|
|
get even more subtotals (all combinations) .. |
|
|
and select the subtotal only |
|
SUM() OVER |
Compute the running sum (balance) of quantity. |
|
RATIO_TO_REPORT |
Percentage and additive percentage … |
|
|
or use a trivial way to compute percentage |
|
RANK |
Use RANK to count top 5 months per product. Get 1,1,3 in case of ties … |
|
DENSE_RANK |
or 1,1,2 with dense_rank … |
|
ROW_NUMBER |
but use row_number to get only one rank 1 in all cases |
|
MAX() OVER |
Find the best month per product. Caution, you get more records in case of more then one record with max
value |
|
|
Use row_number to eliminate this problem |
|
LAG, LEAD |
Get the value from last or next record |
|
RANGE |
Trap again – if there are sparse records (e.g. missing data for a
month you get wrong value. (i.e. value of last/next record, not of last/next
month). Use range to work around it. RANGE defines a logical offset (not a row offset) |
|
NULLS LAST, FIRST |
Define the sort order of NULL as appropriate |
|
RANGE |
Running sum over 3 months |
|
|
Running AVG over 3 months |
|
INTERVAL |
There are two types of interval DAY to SECOND … |
|
|
and YEAR to MONTH |
|
|
A number may by converted to an interval |
|
CUME_DIST |
Top 25% products, i.e. products whose sales are greater or equal than
the sales for 75% of the other products. |
|
|
percentile of the month per product |
|
NTILE |
Divide the months per product in 5 groups based on qty |
|
RANK WITHIN GROUP |
What if scenario |
|
ROW_NUMBER |
Get snapshot from sparse data |
|
LEFT OUTER JOIN |
Get all months and all products. Outer join is not good enough. If
product has no sales in a month it Is not shown. |
|
PARTITIONED BY |
You must use the partitioned outer join to get all combinations |
|
KEEP |
Get initial and final price per product |
|
|
Get a sample data in a GROUP BY query |
Business-Savvy
SQL Oracle Magazine, March-April 2002
Search Ask Tom for “analytic
function” or for “analytics rock”
Jaromir
D.B. Nemec
The author is a freelancer specialized
on Oracle based decision support systems. He can be reached on http://www.db-nemec.com