Calculations

Post on 16-Nov-2014

475 views 0 download

Tags:

transcript

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

ODTUG 2006ODTUG 2006

Michael Armstrong-Smith

Armstrong-Smith Consulting

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

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

Oracle Discoverer 10g Handbook

Released March 2006

Available on Amazon.com or via learndiscoverer.com

Two books available in drawing today

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

http://learndiscoverer.com

New slide – not in handout

http://learndiscoverer.blogspot.com

New slide – not in handout

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

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

Administrators

Create new items

Pre-defined logic

Watch the aggregation

Manage security

Row based

Item based

Creating new items

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.

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!

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).

Profit – workflow 2

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.

Profit – workflow 4

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?

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

Row-based security

Row-based security

What do we need?

Database table

Entries in the table

Database function

A calculation

A mandatory condition

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

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

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);

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;

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

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

Item-based Security

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);

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.

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

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

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

New Data Types

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

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

Basic Requirement

Syntax:

IF Sales > 9,000,000THEN HIGH

ELSE IF Sales > 4,000,000THEN MIDDLE

ELSE LOW

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?

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

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.

New data types – continued

Note the drop-down of new categories

Three new categories

Analytic Functions

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.

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?

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.

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.

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

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.

Analytic Templates

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.

Insert formula from Template

Click here

List of available templates

Analytic Examples

Base Query

Ranking

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

Rank dialog box

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.

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.

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.

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.

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.

Completed Ranking

Running Total

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

Running Total dialog box

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.

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.

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.

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.

Completed Running Total

Top / Bottom n values

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.

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 )

Create Dynamic Parameter

Add a condition that uses the parameter

Build this boolean condition

Ranking <= Top / Bottom

OR

Rank (condition only) <= Top /Bottom

Boolean condition

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

Parameter is prompted

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

Last Year vs This Year

Last Year Example

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

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!

Sequencing

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.

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

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

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

Q & A

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

michael@learndiscoverer.com

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.