Oracle® Reports Building Reports
10g Release 2 (10.1.2) B13895-01 |
|
Previous |
Next |
In this chapter, you will learn about group left formula reports, containing formula columns. By following the steps in this chapter, you can generate the report output shown in Figure 14-1.
Concepts
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.
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.
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 Oracle Reports online Help .
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 allow 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
As you build this example report, you will:
Use the Report Wizard to create a simple report with Group Left style that includes one query that selects all the necessary database columns.
Create two formula columns manually to calculate tax and order totals for each customer, then add the formula columns to your report.
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.
To build the example in this chapter, you must have access to the Order Entry sample schema provided with the Oracle Database. If you do not know if you have access to this sample schema, contact your database administrator.
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:
Launch Reports Builder (or, if already open, choose File > New > Report).
In the Welcome or New Report dialog box, select Use the Report Wizard, then click OK.
If the Welcome page displays, click Next.
On the Report Type page, select Create Paper Layout Only, then click Next.
On the Title page, select Group Left, then click Next.
On the Data Source page, click SQL Query, then click Next.
On the Data page, enter the following SELECT
statement 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) ORDER BY CUSTOMERS_A1.CUSTOMER_ID
Note: You can enter this query in any of the following ways:
|
Click Next.
Note: If you are not already connected to a database, you will be prompted to connect to the database when you click Query Builder or Next. Ensure that you connect to a database that has the appropriate schema for this example. Section 14.1, "Prerequisites for this example" describes the sample schema requirements for this example. |
On the Groups page, select CUSTOMER_ID and click the right arrow (>) to move this field to the Groups Fields list, then click Next.
On the Fields page, click the double right arrow button (>>) to move all of the fields to the Displayed Fields list, then click Next.
On the Totals page, click ORDER_TOTAL and click Sum, then click Next.
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
Save the report as formulareport_
your_initials
.rdf
.
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
To create a formula column that calculates the tax:
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
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. |
Double-click the new formula column object (CF_1) to display the Property Inspector, and set the following 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.
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;
Click Compile.
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.
Close the Property Inspector.
You have created a formula column that calculates the tax (7%) of each order.
To create a formula column that calculates customer order totals:
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.
Change the name of the column to SALES_TOTAL
.
Open the PL/SQL 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;
Click Compile.
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.
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
Now that you have 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:
In the Data Model view, right-click on the canvas, then choose Report Wizard.
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.
Click Finish to display your report output in the Paper Design view. It should look something like this:
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 Oracle Reports online Help, which is available in Reports Builder or hosted on the Oracle Technology Network (OTN), as described in Section 3.1.1, "Using the Oracle Reports online Help".