Skip Headers

Oracle Reports Building Reports 10g (9.0.4) Part Number B10602-01 |
Home | Contents | Index

# 14
Building a Group Left Formula Report

#### Figure 14-1 Formula report output

Text description of the illustration orbrformula_fin.gif

A formula column, like a summary column, is a computational column you create yourself. Unlike a summary, its values are calculated based on a PL/SQL formula you provide. The formula may use data from another column in the report, but is not required to do so.

#### About formula columns

A formula column performs a user-defined computation on another column(s) data, including placeholder columns. Formula columns should not be used to set values for parameters.

#### Concepts

A formula column contains at least one column whose value or values are computed using a PL/SQL formula. Formula columns are similar in usage to summary columns.

For more information on formula columns, refer to the Reports Builder online help (choose Index, then type "formula column" in the box).

##### Data Relationships

- To create a formula report, create a query and select your data. Next, create additional columns and add them to groups in your report. Specify their formulas in the column property sheets.

- As with summaries, you do not select formulas from the database. unlike summaries, which use packaged computations shipped with Oracle Reports, formula columns use formulas you provide by referencing PL/SQL functions. These formulas can be any legal PL/SQL constructs, which allows a great deal of flexibility in the formulas you use.

- A formula performs computations using data from a single record which can span multiple columns. This is in contrast to a summary, which summarizes the data from multiple records in a single column.

##### Layout

- This report uses the default group left format with no modifications.

#### Example Scenario

In this example, you will use the Report Wizard to set up your report and write the one query that selects all the necessary database columns. You will then manually create the two formula columns to calculate tax and order totals for each customer, then add the formula columns to your report. You will use a Group Left style report to make the data in the report easy to read.

To see a sample formula report, open the examples folder called `formula`, then open the Oracle Reports example report named `formula.rdf`. For details on how to open it, see "Accessing the example reports" in the Preface.

##### Table 14-1 Features demonstrated in this example

Feature | Location
--- | ---
Use the Report Wizard to create a report with a paper layout. | Section 14.2, "Use the Report Wizard to create a simple report"
Create two formula columns for the report. | Section 14.3, "Create two formula columns"

## 14.1 Prerequisites for this example

To build the example in this chapter, you must have access to the sample schema provided with the Oracle9i database. If you don't have access to this sample schema, contact your database administrator. You should have access to the "Order Entry" portion of the schema to complete this example.

## 14.2 Use the Report Wizard to create a simple report

When you create a report, you can either use the Report Wizard to assist you or create the report yourself. To build the simple report in this example, you can use the Report Wizard. Using the wizard enables you to define the layout for the report, as well as set the data definition.

#### To create a simple report:

1. Launch Reports Builder (or, if already open, choose File > New > Report)

2. In the Welcome or New Report dialog box, select Use the Report Wizard, then click OK.

3. If the Welcome page displays, click Next.

4. On the Report Type page, select Create Paper Layout Only, then click Next.

5. On the Title page, select Group Left, then click Next.

6. On the Data Source page, click SQL Query, then click Next.

7. On the Data page, click Query Builder.

   Note: Make sure you're connected to the Order Entry portion of the sample schema shipped with the Oracle9i database. If you are already connected to another database, click Connect to connect to the sample schema. When the Connection dialog box displays, enter the connection string for the Order Entry portion of the sample schema provided by your database administrator.

1. In the Select Data Tables dialog box, click CUSTOMERS, then click Include.

2. Click ORDERS, then click Include.

3. Click Close.

4. In Query Builder, in the CUSTOMERS A1 table, select the checkboxes next to the following column:

   - CUSTOMER ID

5. In the ORDERS table, select the checkboxes next to the following columns:

   - ORDER ID

   - ORDER TOTAL

6. Click OK.

7. In the Report Wizard, you should see the following code in the Data Source definition field:

       SELECT ALL CUSTOMERS_A1.CUSTOMER_ID, ORDERS.ORDER_ID, ORDERS.ORDER_TOTAL
       FROM CUSTOMERS CUSTOMERS_A1, ORDERS
       WHERE (ORDERS.CUSTOMER_ID = CUSTOMERS_A1.CUSTOMER_ID)

1. At the end of the code, type the following line:

       ORDER BY CUSTOMERS_A1.CUSTOMER_ID

1. Now, your code should look like this:

       SELECT ALL CUSTOMERS_A1.CUSTOMER_ID, ORDERS.ORDER_ID, ORDERS.ORDER_TOTAL
       FROM CUSTOMERS CUSTOMERS_A1, ORDERS
       WHERE (ORDERS.CUSTOMER_ID = CUSTOMERS_A1.CUSTOMER_ID)
       ORDER BY CUSTOMERS_A1.CUSTOMER_ID

   Note: Although we've shown you how to use the Query Builder to build this query, you can enter this query in any of the following ways: Copy and paste the code from the provided text file called`formula_code.txt` into the Data Source definition field. Click Query Builder to build the query without entering any code manually, as described in the steps above. Type the code in the Data Source definition field.

1. Click Next.

2. On the Groups page, select CUSTOMER_ID and click the right arrow (>) to move this field to the Groups Fields list, then click Next.

3. On the Fields page, click the double right arrow button (>>) to move all of the fields to the Displayed Fields list, then click Next.

4. On the Totals page, click ORDER_TOTAL and click Sum, then click Next.

5. On the Template page, select Predefined Template and click Beige, then click Finish to display your report output in the Paper Design view. It should look something like this:

#### Figure 14-2 Paper Design view for the formula report

Text description of the illustration orbrformula_pap.gif

1. Save the report as formulareport_<your initials>.rdf.

## 14.3 Create two formula columns

Frequently, you want to base calculations on values in your data source. One way you can do this is by using formula columns. The steps in this section will show you how to create two formula columns that calculate the following values:

- the tax on each order

- the grand total for each customer, including tax

### 14.3.1 Create a formula column to calculate the tax

#### To create a formula column that calculates the tax:

1. In Reports Builder, click the Data Model button in the toolbar to display the Data Model view of your report.

   The data model should look something like this:

#### Figure 14-3 Data Model of the Formula Report

Text description of the illustration orbrformula_dm.gif

1. Click the Formula Column tool in the tool palette, then click in the G_ORDER_ID group under ORDER_TOTAL to create a formula column.

   Tip: To view the names of the tools in the tool palette and the toolbar, drag and hold your mouse over each icon and hint text will display describing the icon.

1. Double-click the new formula column object (CF_1) to display the Property Inspector, and set properties:

   - Under General Information, set the Name property to Tax.

   - Under Placeholder/Formula, click the PL/SQL Formula property field to display the PL/SQL Editor.

2. In the PL/SQL Editor, use the template to enter the following PL/SQL code:

       function TAXFormula return Number is
       	tax number;
       begin
         tax := :ORDER_TOTAL * .07;
         return (tax);
       end;

1. Click Compile.

2. If no errors display, click Close. If errors display, verify that your code looks exactly like the above code, paying close attention to the column names.

3. Close the Property Inspector.

You have created a formula column that calculates the tax (7%) of each order.

### 14.3.2 Create a formula column that calculates customer order totals

#### To create a formula column that calculates customer order totals:

1. Repeat Steps 2 and 3 from Section 14.3.1, "Create a formula column to calculate the tax": click the Formula Column tool in the tool palette, then click in the G_ORDER_ID group under TAX. Then, double-click CF_1 to display the Property Inspector.

2. Change the name of the column to `SALES_TOTAL`.

3. Open the Program Unit Editor and modify your formula so that it looks like this:

       function SALES_TOTALFormula return Number is
       	sales_total number;
       begin
         sales_total := :ORDER_TOTAL + :TAX;
         return (sales_total);
       end;

1. Click Compile.

2. If no errors display, click Close. If errors display, verify that your code looks exactly like the above code, paying close attention to the column names.

3. Close the Property Inspector.

You have created a formula column that calculates the total orders of each customer. Your data model should now look something like this:

#### Figure 14-4 Data Model with Formula Columns

Text description of the illustration orbrformula_dm1.gif

### 14.3.3 Add the formula columns to the report layout

Now that you've created your formula columns, you must add them to your report layout. The easiest way to do this is to return to the Report Wizard.

#### To add formula columns to your report layout:

1. In the Data Model view, right-click on the canvas, then choose Report Wizard.

2. In the Report Wizard, click the Fields tab. In the Available Fields list, you should now see your two new formula columns. Click each one, then click the right arrow (>) to move them to the Displayed Fields list.

3. Click Finish to display your report output in the Paper Design view. It should look something like this:

#### Figure 14-5 Final formula report output

Text description of the illustration orbrformula_fin.gif

## 14.4 Summary

Congratulations! You have successfully created a formula paper report. You now know how to:

- define a report layout using the Report Wizard.

- create two formula columns and add them to your report.

For more information on any of the wizards, views, or properties used in this example, refer to the Reports Builder online help, which you can access in two ways:

- From the Oracle Technology Network (`http://otn.oracle.com/products/reports/`), click Documentation and navigate to the Reports Builder Online Help for the most recent, hosted online help.

- From Reports Builder, choose Help > Help Contents.

  Note: You can replace your Reports Builder online help with the most recent update by downloading the latest online help set available on the Oracle Technology Network (http://otn.oracle.com/docs/products/reports/). Instructions for replacing your help file are included in the readme.txt in the download file.

| Copyright © 2002, 2003 Oracle Corporation. All Rights Reserved. |
Home | Contents | Index