+ All Categories
Home > Documents > Calculations

Calculations

Date post: 16-Nov-2014
Category:
Upload: api-27048744
View: 475 times
Download: 0 times
Share this document with a friend
Popular Tags:
90
The art of using calculations The art of using calculations within Oracle Discoverer within Oracle Discoverer ODTUG 2006 ODTUG 2006 Michael Armstrong-Smith Armstrong-Smith Consulting
Transcript
Page 1: Calculations

The art of using calculations within The art of using calculations within Oracle DiscovererOracle Discoverer

ODTUG 2006ODTUG 2006

Michael Armstrong-Smith

Armstrong-Smith Consulting

Page 2: Calculations

Art of using calculations within Discoverer

Data is limited – imagination knows no bounds

Please turn cell phones to mute

If you must make or take a call, please step outside

Page 3: Calculations

About Michael

Member of Oracle Corporation’s customer advisory board for

business intelligence

Co-author of “Oracle Discoverer Handbook”

Presenter at IOUG, OAUG and ODTUG

Fellow of Institute of Analysts and Programmers

MetaLink and OTN Forum panel expert

8 years experience of Discoverer and Oracle Applications

25 years IT experience

Nominated Oracle ACE - 2006

Page 4: Calculations

Oracle Discoverer 10g Handbook

Released March 2006

Available on Amazon.com or via learndiscoverer.com

Two books available in drawing today

Page 5: Calculations

Discoverer Services

ASC is dedicated to Oracle DiscovererOracle Discoverer – all versions

Training – all versions, all platforms– Standard: End User: 3 days, Admin: 2 days– Customized: we train on your data

ConsultingRemote Support

PartnershipsOracleSungard Higher Education

New slide – not in handout

Page 6: Calculations

http://learndiscoverer.com

New slide – not in handout

Page 7: Calculations

http://learndiscoverer.blogspot.com

New slide – not in handout

Page 8: Calculations

Mastering Discoverer

Mastering Discoverer is an art - not a science – and is not something you can just throw a resource at and expect to see a successful, efficient, smooth running environment.

Without adequate training and guidance you will not get the most out of Discoverer.

Mastering calculations is a great step

Page 9: Calculations

Session Objectives

This session covers: Administrators – use calculations to:

Create new itemsManage security

– Row-based– Item-based

End Users – use calculations to:Create new data typesAnalyze data using analytic functions

– Includes an overview of analytic functions

Q&A – 10 minutes at the end

Page 10: Calculations

Administrators

Create new items

Pre-defined logic

Watch the aggregation

Manage security

Row based

Item based

Page 11: Calculations

Creating new items

Page 12: Calculations

Pre-defined algorithms

To create a calculation, use the following workflow:

1. Click on the Data tab.

2. Click on the folder name or click on any item in the folder upon which the calculation will be based.

3. Right click and select New Item.

4. The New Item dialog box will open.

5. Name the calculation.

6. Create the calculation using items and functions.

7. Click OK.

Page 13: Calculations

Profit

In our business area we have two metrics for Selling Price and Cost Price in the Sales folder. The end user tells you that what she needs to see is the difference between the two, the Profit. This is a simple calculation for you to predefine and to the end user it will simply appear as an additional item in the Sales folder.

The really great thing about it is that the end user can select the calculation without adding the items upon which it is based. You will be showered with praise for your brilliance!

Page 14: Calculations

Profit – workflow 1

1. Expand the list of folders in the Sales Department business area.

2. Click on the heading for the Sales folder.

3. Right click and select New Item.

4. The New Item dialog box will open.

5. Name the calculation Profit.

6. Open the Sales folder (notice that the items are displayed in alphabetical order).

Page 15: Calculations

Profit – workflow 2

Page 16: Calculations

Profit – workflow 3

6. Open the Sales folder (notice that the items are displayed in alphabetical order).

7. Select Selling Price and paste it into the Calculation area.

8. Click or type the minus sign.9. Select Cost Price and paste it into the Calculation

area.10. Because we want the calculation to be summed

whenever users select items from the business area, we will use the SUM function to complete the calculation.

Page 17: Calculations

Profit – workflow 4

Page 18: Calculations

Aggregation icon

The new calculation appears as an item at the bottom of the Sales folder. Move it up so that it is below Selling Price.

Notice the new icon for Profit?

Page 19: Calculations

Managing security

Row-based

Prevents a complete row being visible to a user

Item-based

Prevents content of an item being visible to a user

Page 20: Calculations

Row-based security

Page 21: Calculations

Row-based security

What do we need?

Database table

Entries in the table

Database function

A calculation

A mandatory condition

Page 22: Calculations

Security workflow

1. Create the security table

2. Create the security index

3. Grant select rights

4. Populate the table

5. Create a function

6. Import function into Discoverer

7. Create mandatory condition using embedded calculation

8. Test

Page 23: Calculations

Security Table

CREATE TABLE GEN_SECR( USERNAME VARCHAR2(8) NOT NULL, SEC_TYPE VARCHAR2(32) NOT NULL, SEC_IND INTEGER NOT NULL);

In the above table, the three columns are used as follows:USERNAME Oracle usernameSEC_TYPE An identifier for the item to secure.SEC_IND Use 0 for no access, 1 for access

Page 24: Calculations

Security workflow - continued

Create an index:CREATE UNIQUE INDEX GEN_SECR_PK ON

GEN_SECR(USERNAME, SEC_TYPE);

Grant accessGRANT SELECT ON GEN_SECR TO PUBLIC;

Populate the table:INSERT INTO GEN_SECR VALUES ('DRAKE', SALES', 1);

INSERT INTO GEN_SECR VALUES ('MSMITH', ‘SALES', 0);

Page 25: Calculations

Security workflow - continued

CREATE OR REPLACE FUNCTION F_GEN_SEC(SEC_TYPE_IN VARCHAR2)RETURN NUMBER IS

GEN_ACCESS NUMBER := 0;BEGINUSER is a system variable and contains the Oracle user id of the

currently logged in user SELECT SEC_IND INTO GEN_ACCESS FROM GEN_SECR A WHERE A.USERNAME = USER AND A.SEC_TYPE = SEC_TYPE_IN;

RETURN (GEN_ACCESS);EXCEPTION

WHEN NO_DATA_FOUND THENRETURN (GEN_ACCESS);

WHEN OTHERS THENRETURN (GEN_ACCESS);

END F_GEN_SEC;

Page 26: Calculations

Security workflow - continued

Import function into Discoverer Admin:1. Use Tools | Import PL/SQL functions

2. Click Import

3. Locate function to be imported

4. Click OK

5. Click Validate – should be valid

6. Check Arguments

7. Click OK

Page 27: Calculations

Security workflow – continued

Create mandatory condition using embedded calculation

1. Navigate to folder to be protected

2. Right-click in folder, on any item, and from pop-up select New Condition

3. Under Item: select Create Calculation

F_GEN_SEC('SALES') = 1

4. Click OK

5. Test using Discoverer Plus

Page 28: Calculations

Item-based Security

Page 29: Calculations

Item-based security

We will use the same table, but rather than secure a whole table, we will secure an individual item

Let’s secure the Credit Limit and prevent user MSMITH from seeing the content of that item

Populate the table:INSERT INTO GEN_SECR VALUES ('DRAKE', CREDIT', 1);

INSERT INTO GEN_SECR VALUES ('MSMITH', ‘CREDIT', 0);

Page 30: Calculations

Item-based security - workflow

1. Locate and right-click on the item you want to secure

2. From the pop-up menu select Properties

3. Rename the item by adding the characters OLD to the end of the name.

4. Change the Visible to user property to No

5. Click OK to close the Item Properties dialog box.

6. Right-click on the item again, and from the pop-up menu select New Item.

Page 31: Calculations

Item-based security - continued

7. The New Item dialog box will open.

8. Give this new item exactly the same name as the item you renamed in step 3

9. Check the Functions radio button. The Show box will display a list of the function folders. Functions that have been imported into Discoverer are located in the Database folder.

10. Expand the Database folder and select the function you imported earlier

Page 32: Calculations

Item-based security - continued

11. Click Paste. The function specification will be pasted into the Calculation.

12. Complete the calculation using DECODEDECODE(F_GEN_SEC('CREDIT'),1,

Credit Limit OLD,NULL)

13. Click OK to close the New Item dialog box

14. Move the item to its correct location by placing it immediately above the original item

15. Test using Plus

Page 33: Calculations

End Users

Let’s use calculations to:

Create new data types

Analyze data using analytic functionsIncludes an overview of analytic functions

Use of the analytic template

Creating rankings

Creating running totals

Creating top / bottom n values

Creating sequenced analytic operations

Page 34: Calculations

New Data Types

Page 35: Calculations

Creating new data types

Use calculations to create new data types within your worksheet.

These new types only exist within the worksheet.After you have create a new data type, you can:

Create sortsCreate totalsCreate other calculations.

Let’s say we want to generate a new data type called Category, and this category has three valuesHighMiddleLow

Page 36: Calculations

New data types - continued

We want to analyze our sales and work out our top selling customers, such that customers with:

Sales > 9 million are High

Sales > 4 million but <= 9 million are MiddleSales <= 4 million are Low

Page 37: Calculations

Basic Requirement

Syntax:

IF Sales > 9,000,000THEN HIGH

ELSE IF Sales > 4,000,000THEN MIDDLE

ELSE LOW

Page 38: Calculations

A WHEN – WHEN situation

Use a CASE statementCASE

WHEN Sales > 9000000 THEN ‘High’

WHEN Sales > 4000000 THEN ‘Middle’

ELSE ‘Low’

END

Is this a WHEN – WHEN or a WIN – WIN situation?

Page 39: Calculations

Let’s do it using an existing workbook

Launch the New Calculation dialog box.

1. Name the calculation Category.

2. From the Show drop-down list, select Functions.

3. Scroll down then expand Others.

4. Select the CASE function and paste into the calculation

Page 40: Calculations

New data types – continued

5. The calculation should look like this:

6. Click OK to close the New Calculation dialog box.

7. Drag the Category to the Page Items.

Page 41: Calculations

New data types – continued

Note the drop-down of new categories

Three new categories

Page 42: Calculations

Analytic Functions

Page 43: Calculations

Using analytic functions

Analytic functions are functions that run against the data in a query after the query has computed its normal results.Whereas standard functions and calculations, such as profit, execute on a row-by-row basis as the query runs, analytic functions run against the results.They support basic business intelligence calculations such as moving averages, rankings, and running totals.

Page 44: Calculations

Typical business questions

What is my best performing product in terms of sales?What are my top n / bottom n selling products?How do sales this year compare to last year?What is the 3 month moving average of sales?What products sell more than 20% of total sales for their product line?Of my top 10 products, how many open orders do I have?

Page 45: Calculations

Categorizing analytic functions

Analytic functions fall into the following categories:

Ranking - used to calculate the rank of a value in a result set.

Window Aggregates - used to calculate cumulative sums, running totals and moving averages.

Reporting Aggregates - can be used to calculate market share.

Lag / Lead - can be used to find a value in a row offset from a current row. Perhaps this year compared to last year.

Page 46: Calculations

Processing Order

When analytic functions are in use, query processing takes place in three stages:

All joins, WHERE, GROUP BY and HAVING clauses are performed first.

The resulting set of data is made available to the analytic functions and those calculations take place.

If the query contains a sort, this is processed.

Page 47: Calculations

Partitions

An important concept in the use of analytics is to define a partition:

A partition is a set of data that has been ordered into a group of rows.

Discoverer page items and group sorts form natural partitions.

Note: this term is unrelated to database table partitions

Page 48: Calculations

Building effective analytics

In order to build a Discoverer worksheet that makes use of analytical functions, you need to build worksheets in the following order:

Build the base worksheet.

Add Page Items and / or Group Sorts.

Define the analytic function.

Create the remaining sorts.

Page 49: Calculations

Analytic Templates

Page 50: Calculations

Using analytic templates

In older releases of Discoverer, and in all Desktop versions, even though the use of analytic functions is supported, you have to manually type the function yourself.

In recent editions of Discoverer Plus, Oracle has inserted a new button on the New Calculation dialog box called Insert Formula from Template.

Page 51: Calculations

Insert formula from Template

Click here

Page 52: Calculations

List of available templates

Page 53: Calculations

Analytic Examples

Page 54: Calculations

Base Query

Page 55: Calculations

Ranking

Page 56: Calculations

Ranking

Start out from our base query

Launch the New Calculation dialog box

Click the Insert Formula from Template button.

From the list of available templates, select Rank.

Discoverer opens the Rank dialog box

Page 57: Calculations

Rank dialog box

Page 58: Calculations

Rank dialog box explained - 1

Rank based onUse this area to define the item(s) on which the ranking should be based.Discoverer will always try to populate this area for you.By default, it will use the first data point that it finds. If this is not the one that you want to rank by, change it.You can also state whether the highest value or the lowest value should be used to start the ranking.

Page 59: Calculations

Rank dialog box explained - 2

Restart ranking at each change on

This is the partition area. Discoverer will automatically populate this area using the Page Items and Group Sorts that you are using.

As you can see on screen, we have a Group Sort based on the Product Size. Discoverer has automatically brought this into the formula.

Discoverer will restart the ranking according to the items that you place in here, inserting these items into the PARTITION BY clause of the SQL.

Page 60: Calculations

Rank dialog box explained - 3

Rank Ties

This area is used to tell Discoverer whether to use standard ranking or dense ranking.

A standard ranking will omit a number when there is a tie for the preceding number.

A dense ranking ranks all possible values are used.

Page 61: Calculations

Rank dialog box explained - 4

Calculation

This area is used by Discoverer to display the SQL code that will be generated when you click OK.

You cannot manually change this code at this time. To change the code, you need to use the template.

Page 62: Calculations

Completing the Ranking

Once you have completed the template, clicking OK causes Discoverer to create the function.Discoverer places a copy of the SQL into the New Calculation dialog box.Assuming you have defined the function correctly, all that remains now is for you to give the function a name and click OK.After completing the function, you should check the format for both the heading and the data.We typically center all of our rank functions and sort by the rank.

Page 63: Calculations

Completed Ranking

Page 64: Calculations

Running Total

Page 65: Calculations

Running Total

Staying in our query

Launch the New Calculation dialog box

Click the Insert Formula from Template button.

From the list of available templates, select Running Total.

Discoverer opens the Running Total dialog box

Page 66: Calculations

Running Total dialog box

Page 67: Calculations

Running Total dialog box explained - 1

Running Total based on

Use this area to define the item(s) on which the running total should be based.

Discoverer will always try to populate this area for you.

By default, it will use the first data point that it finds. If this is not the one that you want to total by, change it.

Page 68: Calculations

Running Total dialog box explained - 2

Order rows by

Use this area to define the item(s) that you want to order the query by.

This will typically, but not always, be the same item that is being used for the running total calculation itself.

In our case, Discoverer has picked our ranking analytic function because we placed a sort on it.

Page 69: Calculations

Running Total dialog box explained - 3

Restart running total at each change on

This is the partition area. Discoverer will automatically populate this area using the Page Items and Group Sorts that you are using.

As you saw on screen, we have a Group Sort based on the Product Size. Discoverer has automatically brought this into the formula.

Discoverer will restart the running total according to the items that you place in here, inserting these items into the PARTITION BY clause of the SQL.

Page 70: Calculations

Running Total dialog box explained - 4

CalculationThis area is used by Discoverer to display the SQL code that will be generated when you click OK.

You cannot manually change this code at this time. To change the code, you need to use the template.

Page 71: Calculations

Completed Running Total

Page 72: Calculations

Top / Bottom n values

Page 73: Calculations

Top / bottom n selling products

Use this workflow:Create another calculation, identical to the ranking calculation, except that the order by is reversed.

This will only be used in a condition.

Do not allow the second ranking to display on screen.

Add a dynamic parameter that prompts user for the top / bottom n value.

Page 74: Calculations

The two SQL snippets

First rankingRANK() OVER(PARTITION BY Size

ORDER BY Profit DESC )

Second ranking (condition only)RANK() OVER(PARTITION BY Size

ORDER BY Profit ASC )

Page 75: Calculations

Create Dynamic Parameter

Page 76: Calculations

Add a condition that uses the parameter

Build this boolean condition

Ranking <= Top / Bottom

OR

Rank (condition only) <= Top /Bottom

Page 77: Calculations

Boolean condition

Note: Because the Top / Bottom dynamic parameter is now being referenced it will now be prompted

Page 78: Calculations

Parameter is prompted

Page 79: Calculations

Run the query with 2

Notice how only the top and bottom 2 values have been returned but that the bottom line is still correct

Page 80: Calculations

Last Year vs This Year

Page 81: Calculations

Last Year Example

In this example, we have a worksheet that shows total sales for this and last year.

Page 82: Calculations

Last Year’s calculation

In case you are interested:

SUM(Selling Price SUM) OVER(PARTITION BY Size,Product ORDER BY TO_DATE("Year",'YYYY') RANGE BETWEEN INTERVAL '1' YEAR PRECEDING AND INTERVAL '1' YEAR PRECEDING )

When RANGE INTERVAL = YEAR the ORDER BY must return a date!

Page 83: Calculations

Sequencing

Page 84: Calculations

Sequencing Conditions

The power and flexibility of Discoverer enables almost unlimited analytic capabilities.

As an example, lets start with our report that returns the top 10 products by size.

Page 85: Calculations

Non-sequenced Conditions

Create two new conditions:

Condition 1: Ranking <= 10

Condition 2: Status = OPEN

There is an implied AND clause between the conditions with the non-analytic being applied first

Page 86: Calculations

Sequenced conditions

Create a single boolean conditionRanking <= 10

AND

Status = OPEN

In a boolean condition, the analytic component is applied first, thus sequencing the conditions

Page 87: Calculations

Benefits of using Discoverer

There are a number of benefits to using Discoverer to create reports using analytic functions.

The Oracle database does not support analytic functions directly referencing other analytic functions (nesting) or directly applying conditions to the query (e.g. only show Top N products).To achieve this in a SQL statement requires the creation of inline views.Discoverer removes this layer of complexity from the end user by breaking down the query into it’s components and resolving such conflicts by creating the appropriate inline views

Page 88: Calculations

Q & A

Page 89: Calculations

Thank You

Please remember to complete your evaluation form

Presenter - Michael Armstrong-Smith

Session name – Art of using calculations within Discoverer

For more information or to get help about Discoverer 10.1.2 please contact Michael

[email protected]

Page 90: Calculations

Copyright

The content provided as papers and presentations from the ODTUG conferences is copyrighted by the authors, and has been licensed to the ODTUG. It is only authorized for the personal use of ODTUG members and ODTUG conference attendees directly through the ODTUG web site.

Downloading the files, placing them on other web sites, sharing them with other individuals or third parties is prohibited, unless permission to do so has been granted by the authors or ODTUG.


Recommended