Skip Headers

Oracle Discoverer Desktop User's Guide
10g (9.0.4) for Windows

Part Number B10272-01
Go To Documentation Library
Home
Go To Table Of Contents
Contents
Go To Index
Index

Go to previous page Go to next page

A
Calculation Examples

This appendix contains the following sections:

A.1 Getting More Information

For more information about Analytic Functions, and Oracle functions in general, refer to the following Oracle publications:

A.2 About the examples in this chapter

The examples in the following sections use the Video Stores Tutorial.

A.3 Additional formatting

The examples in this chapter use the following additional formatting:

A.4 Simple Calculation Examples

This section provides the following examples to help you create Calculations.


NOTE: For more information on how to create Calculations, refer to Section 8.3, "Creating Calculations."


A.4.1 Calculate the number of rows returned by a query

This example calculates the number of rows returned by a query.

Workbook options  Details 

Select Items 

Video Analysis Information: Year (Calendar Year), Region, City, Sales SUM 

Sort Order 

Year, Region, City 

Conditions 

Department = Video Sale OR Department = Video Rental

Year = 2000 AND Region = Central 

Calculation Name 

Rows returned 

Calculation 

ROWCOUNT 

Notes 

ROWCOUNT does not count NULL values. To calculate the number of rows returned by a query, including NULL values, first create a temporary item One record, (Calculation = `1').
Then, create a Calculation Rows returned to count the occurrences of One record, (SUM(Video Sales Analysis.One record)) 

Display Data 

Text description of simple2.gif follows.
Text description of the illustration simple2.gif
 

A.4.2 Calculate a 25% increase in sales

This example calculates a 25% increase in sales figures.

Workbook options  Details 

Select Items 

Video Analysis Information: Year (Calendar Year), Region, City, Sales SUM 

Sort Order 

Year, Region, City 

Conditions 

Department = Video Sale OR Department = Video Rental

Year = 2000 AND Region = Central 

Calculation Name 

25% Increase 

Calculation 

Sales SUM * 1.25 

Display Data 

Text description of simple1.gif follows.
Text description of the illustration simple1.gif
 

A.4.3 Convert text to upper-case

As well as the extensive range of mathematical functions available in Discoverer, you also have access to a wide range of number and text formatting functions. This example uses a Calculation to re-format City text data to upper-case.

Workbook options  Details 

Select Items 

Video Analysis Information: Year (Calendar Year), Region, City, Sales SUM 

Sort Order 

Year, Region 

Conditions 

Department = Video Sale OR Department = Video Rental

Year = 2000 AND Region = Central 

Calculation Name 

City(Upper Case) 

Calculation 

UPPER(City) 

Display Data 

Text description of simple3.gif follows.
Text description of the illustration simple3.gif
 

A.5 Analytic Function Examples

Oracle Discoverer supports the Analytic Functions that are included in Oracle Server 8.1.6 and above. Discoverer also extends the functionality of standard SQL by allowing you to nest Analytic Functions. You can use these advanced functions to perform sophisticated analysis on your data.

This section contains the following sub-sections:

A.5.1 Analytic Function Categories

Analytic Functions are classified in the following categories:

A.5.2 Calculations and Drilling Into and out of data

When you use Analytic Functions, note that they have a precise definition which does not change as you drill, pivot, or sort the result set. For example, if you use the RANK function to assign ranks to sales figures partitioned by Quarter, if you drill down the Month level, the rank still only applies to the Quarter level.

A.5.3 About The Analytic Function Template

When creating Analytic Functions in Discoverer, you can either type or paste them directly into the Calculation dialog box, or you can select them from the function list.

If you select them from the function list, you are presented with a generic Analytic Function Template that helps you define the function by telling you what information you may need to provide. Templates should be used as a guide. Because templates are designed to cover most types of usage, you will not always need to use every part of the template.

For example, when you paste a new RANK Analytic Function into a calculation box, Discoverer provides the following template:

Although you can define a complex function using both expressions (expr1 and expr2), you can often define a simple function using only the ORDER BY expression; for example:

NOTE: By default, results data is sorted in ascending order (ASC), nulls first (NULLS FIRST).

For more information about expressions used by the Analytic Function template, refer to "More about the Analytic Function Template".

A.5.4 Ranking Function Examples

A.5.4.1 About Ranking

Ranking functions compute the rank of an item with respect to other items in an ordered list.

A.5.4.2 Assign Ranks to sales figures

This example ranks to a set of sales figures.

Workbook options  Details 

Select Items 

Video Analysis Information: Year (Calendar Year), Region, City, Sales SUM 

Sort Order 

Year, Region 

Conditions 

Department = Video Sale OR Department = Video Rental

Year = 2000 AND Region = Central 

Calculation Name 

Rank 

Calculation 

RANK() OVER(ORDER BY Sales SUM DESC) 

Notes 

By default, ranked results data is sorted in ascending order (ASC), nulls first (NULLS FIRST). The additional DESC parameter sorts the results in descending order, which ranks the highest value with the Rank 1. 

Display Data 

Text description of rank.gif follows.
Text description of the illustration rank.gif
 

A.5.4.3 Assign Ranks to sales figures within Region

This example ranks a set of sales figures within each Region for each Year.

Workbook options  Details 

Select Items 

Video Analysis Information: Year (Calendar Year), Region, City, Sales SUM 

Sort Order 

Year, Region 

Conditions 

Department = Video Sale OR Department = Video Rental

Year = 2000 

Calculation Name 

Rank Top 

Calculation 

RANK() OVER(PARTITION BY Year, Region ORDER BY Sales SUM DESC) 

Display Data 

Text description of rsubg.gif follows.
Text description of the illustration rsubg.gif
 

A.5.4.4 Show the top three selling Cities per Region

This example ranks a set of sales figures and displays the top three selling Cities for each Region.

Workbook options  Details 

Select Items 

Video Analysis Information: Year (Calendar Year), Region, City, Sales SUM 

Sort Order 

Year, Region 

Conditions 

Department = Video Sale OR Department = Video Rental

Year = 2000

Rank Top <= 3 

Calculation Name 

Rank Top 

Calculation 

RANK() OVER(PARTITION BY Year, Region ORDER BY Sales SUM DESC) 

Tip 

To quickly filter the list to the first, second, or third ranked cities, pivot the Rank Top item to the page axis, (see also Section 4.1.2, "Pivoting Data on a Table"). 

Display Data 

Text description of rank_top.gif follows.
Text description of the illustration rank_top.gif
 

A.5.4.5 Show the top three and bottom three selling Cities per Region

This example ranks a set of sales figures and displays the top three and bottom three performing Cities per Region.

Workbook options  Details 

Select Items 

Video Analysis Information: Year (Calendar Year), Region, City, Sales SUM 

Sort Order 

Year, Region 

Conditions 

Department = Video Sale OR Department = Video Rental

Year = 2000

Rank Top <= 3 OR Rank Bottom <= 3 

Calculation Name 

Rank Top 

Calculation 

RANK() OVER(PARTITION BY Year, Region ORDER BY Sales SUM DESC) 

Additional Calculations Required 

Rank Bottom = RANK() OVER(PARTITION BY Year, Region ORDER BY Sales SUM ASC) 

Notes 

This analysis involves three steps:

1 - Assign ranks to Cities on Sales SUM in descending order, as Rank Top.

2 - Assign ranks to Cities on Sales SUM in ascending order, as Rank Bottom.

3 - Displaying only Rank Top, filter the data using a Condition to return only the top three and bottom three ranked Brands.

In the example shown below, in the `Central' Region, the top three cities are ranked 1, 2, and 3; the bottom three cities are ranked 5, 6, and 7.
In the `East' Region, the top three cities are ranked 1, 2, and 3; the bottom three cities are ranked 6, 7, and 8, and so on.
 

Display Data 

Text description of rtb3.gif follows.
Text description of the illustration rtb3.gif
 

A.5.5 Banding Function Examples

A.5.5.1 About Banding

Banding is a type of ranking that divides a list of values in a partition into a specified number of groups called Bands (also known as buckets) and assigns each value to a Band.

Two common types of banding are:

A.5.5.2 Producing equi-width Bands (1)

This example divides Sales figures into bands according to their value, (also known as equi-width bands).

Workbook options  Details 

Select Items 

Video Analysis Information: Year (Calendar Year), Region, City, Sales SUM 

Sort Order 

Year, Region 

Conditions 

Department = Video Sale OR Department = Video Rental

Year = 2000, Region = Central 

Calculation Name 

Sales Bands 

Calculation 

GREATEST(1,4-FLOOR((Sales SUM-Min Sales for Region)/GREATEST(1,FLOOR((Max Sales for Region-Min Sales for Region+1)/4)))) 

Additional Calculations Required 

Max Sales for Region = MAX(Sales SUM) OVER(PARTITION BY Region,Year)

Min Sales for Region = MIN(Sales SUM) OVER(PARTITION BY Region,Year) 

Notes 

Using the Central Region and Year 2000 as an example, this function takes the largest value (45,758) minus the smallest value (7,749) and divides it by four, ((45,758-7,749)/4), giving four equal Bands of 9,502.25. This gives four bands with the following ranges:

  • Band 1 - 36,255.75 to 45,758

  • Band 2 - 26,753.5 to 36,255.75

  • Band 3 - 17,251.25 to 26,753.5

  • Band 4 - 7,749 to 17,251.25

Each value is placed in one of the four Bands depending on which range the Sales SUM value falls into. 

Display Data 

Text description of beqw.gif follows.
Text description of the illustration beqw.gif
 

A.5.5.3 Producing equi-width Bands (2)

This example creates the same results as the example in "Producing equi-width Bands (1)", except that it uses a CASE statement rather than the GREATEST function.
Again, the example divides Sales figures into bands according to their value, using a CASE function, (see also "Producing equi-width Bands (1)").

Workbook options  Details 

Select Items 

Video Analysis Information: Year (Calendar Year), Region, City, Sales SUM 

Sort Order 

Year, Region 

Conditions 

Department = Video Sale OR Department = Video Rental

Year = 2000, Region = Central 

Calculation Name 

Sales Bands 2 

Calculation 

CASE
WHEN Sales SUM < Q1 THEN 4
WHEN Sales SUM < Q2 THEN 3
WHEN Sales SUM < Q3 THEN 2
WHEN Sales SUM >= Q3 THEN 1
END 

Additional Calculations Required 

MAX Sales = MAX(Sales SUM) OVER(PARTITION BY Year)

MIN Sales = MIN(Sales SUM) OVER(PARTITION BY Year)

Range = (MAX Sales - MIN Sales)/4

Q1 = MIN Sales + Range

Q2 = MIN Sales + (Range*2)

Q3 = MAX Sales - Range 

Notes 

This function uses a series of IF statements in the form of a CASE function to assign sales figures into bands, (see Band Ranges below).

Text description of append14.gif follows.
Text description of the illustration append14.gif
 

Display Data

 

Text description of beqw2.gif follows.
Text description of the illustration beqw2.gif
 

A.5.5.4 Producing equi-height Bands

This example assigns a set of sales figures into two equi-height Bands.

Workbook options  Details 

Select Items 

Video Analysis Information: Year (Calendar Year), Region, City, Sales SUM 

Sort Order 

Year, Region 

Conditions 

Department = Video Sale OR Department = Video Rental

Year = 2000, Region = Central 

Calculation Name 

Sales Bands 3 

Calculation 

NTILE(2) OVER(PARTITION BY Year, Region ORDER BY Sales SUM DESC) 

Notes 

Using the Central Region and Year 2000 as an example, this function takes the number of values (which is six) and divides it by two, giving three values per Band. It then takes the list of values ordered by Sales SUM and places values one, two, and three in Band 1, values four, five, and six in Band 2. 

Display Data 

Text description of beqh.gif follows.
Text description of the illustration beqh.gif
 

A.5.6 Windowing Function Examples

A.5.6.1 About Windowing

Windowing functions are used to compute aggregates using values from other rows. For example, cumulative, moving, and centered aggregates.

Two common types of windowing are:

A.5.6.2 Calculate a three month moving sales average

This example uses a logical window to calculate a moving three month Sales average.

Workbook options  Details 

Select Items 

Video Analysis Information: Year (Calendar Year), Region, City, Sales SUM 

Sort Order 

Year, Region 

Conditions 

Department = Video Sale OR Department = Video Rental

Year = 2000, Region = Central 

Calculation Name 

Sales Bands 3 

Calculation 

NTILE(2) OVER(PARTITION BY Year, Region ORDER BY Sales SUM DESC) 

Notes 

Using the Central Region and Year 2000 as an example, this function takes the number of values (which is six) and divides it by two, giving three values per Band. It then takes the list of values ordered by Sales SUM and places values one, two, and three in Band 1, values four, five, and six in Band 2. 

Display Data 

Text description of beqh.gif follows.
Text description of the illustration beqh.gif
 

A.5.6.3 Show the cumulative values of sales

This example uses a physical window to calculate the cumulative value of sales.

Workbook options  Details 

Select Items 

Video Analysis Information: Year (Calendar Year), Region, City, Sales SUM 

Sort Order 

Year, Region 

Conditions 

Department = Video Sale OR Department = Video Rental

Year = 2000, Region = Central 

Calculation Name 

Cumulative Total 

Calculation 

SUM(Sales SUM) OVER(ORDER BY Sales SUM DESC ROWS UNBOUNDED PRECEDING) 

Display Data 

Text description of cump2.gif follows.
Text description of the illustration cump2.gif
 

A.5.7 Reporting Function Examples

A.5.7.1 About Reporting Functions

Reporting functions are used to compute aggregates.

A.5.7.2 Calculate annual sales by Year

This example calculates annual sales by Year.

Workbook options  Details 

Select Items 

Video Analysis Information: Year (Calendar Year), Region, City, Sales SUM 

Sort Order 

Year, Region 

Conditions 

Department = Video Sale OR Department = Video Rental

Year = 2000 

Calculation Name 

Annual Sales 

Calculation 

SUM(Sales SUM) OVER() 

Display Data 

Text description of reportin.gif follows.
Text description of the illustration reportin.gif
 

A.5.7.3 Calculate annual sales by region

This example calculates the total annual sales by Year and Region.

Workbook options  Details 

Select Items 

Video Analysis Information: Year (Calendar Year), Region, City, Sales SUM 

Sort Order 

Year, Region 

Conditions 

Department = Video Sale OR Department = Video Rental

Year = 2000 

Calculation Name 

Annual Sales by Region 

Calculation 

SUM(Sales SUM) OVER(PARTITION BY Year, Region ORDER BY Year, Region) 

Display Data 

Text description of reportia.gif follows.
Text description of the illustration reportia.gif
 

A.5.7.4 Calculate percentage of annual sales by Region

This example calculates the percentage of annual sales per Region for each City in each Year.

Workbook options  Details 

Select Items 

Video Analysis Information: Year (Calendar Year), Region, City, Sales SUM 

Sort Order 

Year, Region, % of Annual Sales 

Conditions 

Department = Video Sale OR Department = Video Rental

Year = 2000 

Calculation Name 

% of Annual Sales 

Calculation 

Sales SUM*100/Annual Sales by Region 

Additional Calculations Required 

Annual Sales by Region= SUM(Sales SUM) OVER(PARTITION BY Year, Region ORDER BY Year, Region) 

Display Data 

Text description of reportib.gif follows.
Text description of the illustration reportib.gif
 

A.5.7.5 Calculate sales as a percentage of total Sales

This example calculates sales as a percentage of total Sales.

Workbook options  Details 

Select Items 

Video Analysis Information: Year (Calendar Year), Region, City, Sales SUM 

Sort Order 

Year, Region, % of total Sales 

Conditions 

Department = Video Sale OR Department = Video Rental

Year = 2000, Region = Central 

Calculation Name 

% of total Sales 

Calculation 

RATIO_TO_REPORT(Sales SUM) OVER()*100 

Display Data 

Text description of reportic.gif follows.
Text description of the illustration reportic.gif
 

A.5.8 Lag/Lead Function Examples

A.5.8.1 About Lag/Lead Functions

LAG and LEAD functions are typically used to compare values in different time periods. For example, compare sales figures in 2000 with sales figures in 2001.

A.5.8.2 Compare sales figures across time

This example compares sales figures across time.

Workbook options  Details 

Select Items 

Video Analysis Information: Year (Calendar Year), Sales SUM 

Sort Order 

Year 

Conditions 

Department = Video Sale OR Department = Video Rental

Region = Central 

Calculation Name 

Previous Year 

Calculation 

LAG(Sales SUM,1) OVER(ORDER BY Year) 

Notes 

Because there are no comparative figures for 1998, the Previous Year value for 1998 is blank. 

Display Data 

Text description of laglead1.gif follows.
Text description of the illustration laglead1.gif
 

A.5.8.3 Calculate sales growth across time

Using the comparative sales figures from example 11.8.2, this example calculates the sales growth across time.

Workbook options  Details 

Select Items 

Video Analysis Information: Year (Calendar Year), Sales SUM 

Sort Order 

Year 

Conditions 

Department = Video Sale OR Department = Video Rental

Region = Central 

Calculation Name 

Growth 

Calculation 

(Sales SUM-Previous Year)*100/Previous Year 

Additional Calculations Required 

Previous Year = LAG(Sales SUM,1) OVER(ORDER BY Year) 

Notes 

Because there are no comparative figures for 1998, the Previous Year value for 1998 is blank. 

Display Data 

Text description of laglead2.gif follows.
Text description of the illustration laglead2.gif
 

A.5.8.4 Rank sales growth

Using the comparative sales figures from examples A.2.8.2 and A.2.8.3, this example ranks sales growth by year.

Workbook options  Details 

Select Items 

Video Analysis Information: Year (Calendar Year), Sales SUM 

Sort Order 

Year 

Conditions 

Department = Video Sale OR Department = Video Rental

Region = Central 

Calculation Name 

Rank Growth 

Calculation 

RANK() OVER(ORDER BY Growth DESC) 

Additional Calculations Required 

Previous Year = LAG(Sales SUM,1) OVER(PARTITION BY 'Year' ORDER BY 'Year' )

Growth = (Sales SUM-Previous Year)*100/Previous Year 

Notes 

Because there are no comparative figures for 1998, the Previous Year and Growth values for 1998 are blank and the Rank Growth is calculated as `1'. 

Display Data 

Text description of laglead4.gif follows.
Text description of the illustration laglead4.gif
 

A.5.9 Statistical Function Examples

A.5.9.1 About Statistics Functions

Statistics functions are used to compute covariance, correlation, and linear regression statistics. Each function operates on an unordered set. They also can be used as windowing and reporting functions.

A.5.9.2 Calculate linear regression

This example computes an ordinary least-squares regression line that expresses the Profit SUM per Month as a linear function of its Sales SUM. The following functions are used:

A.6 More about the Analytic Function Template

When you paste a new Analytic Function into a Calculation box, Discoverer provides the following generic template:

The expressions are used as follows.

For more information about Oracle expressions, refer to "Getting More Information".

A.7 Analytic Functions and Sequencing

When you use Analytic Functions in Conditions, the way that you combine them with non-Analytic Functions affects the Discoverer data returned by the query. The following sequencing rules apply:

To illustrate how sequencing affects the Discoverer data returned by a query, consider the following two scenarios.


Text description of append29.gif follows.
Text description of the illustration append29.gif

In the first scenario above, we apply two simple Conditions: Region = `Central', and Rank <= 3 (where Rank is an Analytic Function). The Region = `Central' condition is applied first, then Rank <= 3. Therefore, only Sales figures for the Central Region that have a ranking of three or less are included in the Results Set.


Text description of append30.gif follows.
Text description of the illustration append30.gif

In the second scenario above, we apply a single advanced Condition: Region = `Central' AND Rank <= 3, (where Rank is an Analytic Function). The Rank <= 3 condition is applied first, then the Region = `Central' condition. Therefore, only figures in the Central Region that have an overall ranking of three or less are included in the Results Set.


Go to previous page Go to next page
Oracle
Copyright © 1996, 2003 Oracle Corporation.

All Rights Reserved.
Go To Table Of Contents
Contents
Go To Index
Index