So far, the reports you have created have returned all the records from your database. Sometimes this is appropriate, but often reports need to filter the data based on specified criteria. This is most relevant when you're working with large databases in which there can easily be hundreds of thousands of records returned from a query, especially when joins are applied.
As with many features in Crystal Reports, there are multiple ways to filter data:
Regardless of the method used to filter your report, you should always make best efforts to filter on indexed database fields. By filtering on indexed fields, you realize the greatest performance on the database server. You can determine the indexed fields in a table by using the Crystal Reports Links tab on the Database Expert accessible from the Database menu. Use the Index Legend button and dialog provided to understand the different index markers in your database tables.
Working with the Select Expert
The Select Expert is a design tool that enables you, the report designer, to specify basic yet powerful filters for the current report using a graphical design dialog. Figure 3.1 shows the Select Expert dialog. Let's work through an illustrative example of filtering using the Select Expert. Taking what you have learned so far about creating simple columnar reports, create a new report from the Xtreme Sample Database 10, adding the Customer Name and Last Year's Sales fields from the Customer table to the details section of the report. Follow these steps to add a filter to this report:
TIP
Another quick and directed method of accessing the Select Expert is through the right-click menu available on any data field. This method opens the Select Expert directly with the specified field already selected as the filtered field and bypassing the Choose Field dialog.
TIP
Often when modifying filters and selections in the report designer, Crystal Reports displays a message asking the user if she wants to use the saved data in the report or refresh the data from the database. Using the saved data in the report is usually a good option because it does not incur a new query to the database. However, especially when modifying filters, it can cause some confusing results because the set of saved data in the report might or might not consist of all the records in the database; that is, a filter might have already been applied. So when modifying filters, it's best to refresh the data whenever Crystal Reports asks you.
Figure 3.1. The Select Expert provides access to easy-to-use filtering functionality.
Let's look at a few more types of filters that can be applied to a report. The following steps walk you through applying these various types of filters:
Figure 3.2. Modify the report to display customers with sales between $2,000 and $3,000.
NOTE
When using the Is Like option, an * acts as a wildcard for any number of characters, whereas a ? acts as a wildcard for only a single character. This can be quite useful when you're searching through textual fields for a specific text pattern.
The last thing this chapter covers with respect to the Select Expert is applying multiple filters. To do so, perform the following steps:
Figure 3.3. A filter is applied to show all Canadian customers with sales less than $5,000.
NOTE
The two filters that were just added to the report are concatenated together by default with a logical AND statement, that is, All Customers with Last Year's Sales of less than $5,000 AND from Canada. This can be edited in the Formula Editor accessible from the Show Formula button on the Select Expert. This is discussed in the next section.
The Record Selection Formula
Although the Select Expert is quite powerful, there are certain situations where you need to define a filter that is more complex than the Select Expert allows. Fortunately, Crystal Reports has a built-in formula language that enables custom expressions to be defined as a filter. In fact, this is one of the strengths of the Crystal Reports product: being able to use the formula language to attain a high level of control in various aspects of report creation.
Although you might not have realized it, even when you were using the Select Expert, a formula was being generated in the background that defined the filter. To see this in action, open the Select Expert and click the Show Formula button. This expands the Select Expert dialog to reveal the formula being generated. This formula is called the record selection formula. Notice that the formula's value is as follows:
{Customer.Last Year's Sales} < $5000.00 and {Customer.Country} = "Canada"
The formula language is covered in more detail in Chapter 11, "Using Record Selections and Alerts for Interactive Reporting," but the following are the key points to learn right now. In formulas, braces denote a field. For database fields, the table and field name are included and are separated by a period. The rest of the formula is a statement that tests whether the sales value is more than $5,000.
Think of a record selection formula as an expression that evaluates to a true or false result. For each record in the database, Crystal Reports applies the record selection formula, plugging in the current field values in place of the fields in braces. If the result of the statement is True, the record is included in the report. If the result of the statement is False, the record is excluded from the report. Let's look at an example. The first record in the Customer's table is that of City Cyclists who had sales of $20,045.27.
For this record, Crystal Reports evaluates the preceding formula, substituting $20,045.27 in place of {Customer.Last Year's Sales}. Because this value is larger than $5,000, this statement is False and the record is not included in the report. To see what other formulas look like, change the filter using the Select Expert to a few different settings and observe how the formula changes.
Working with the Formula Editor
The formula shown at the bottom of the Select Expert is not just for informational purposes: It can be edited in-place. However, a much better editor exists for formulas. It's called the Formula Editor (shown in Figure 3.4), and it can be invoked by clicking the Formula Editor button in the Select Expert or by selecting the Report menu and choosing Selection Formulas, Record. Although the formula language doesn't change, the process of creating formulas becomes much simpler because of a focused user interface.
Figure 3.4. The Formula Editor provides quick access to powerful formula creation capabilities.
Let's work through creating a simple record selection formula. This formula attempts to filter out any customers who owe more than $5,000 in tax. Tax owing will be defined as 2% of their sales figure. To implement this, work through the following steps:
{Customer.Last Year's Sales} * 0.02
({Customer.Last Year's Sales} * 0.02) > 5000
Both the formula language and the Formula Editor are topics unto themselves and will be discussed in more detail in Chapter 4, "Understanding and Implementing Formulas," and Chapter 11, "Using Record Selections and Alerts for Interactive Reporting."
Part I. Crystal Reports Design
Creating and Designing Basic Reports
Selecting and Grouping Data
Filtering, Sorting, and Summarizing Data
Understanding and Implementing Formulas
Implementing Parameters for Dynamic Reporting
Part II. Formatting Crystal Reports
Fundamentals of Report Formatting
Working with Report Sections
Visualizing Your Data with Charts and Maps
Custom Formatting Techniques
Part III. Advanced Crystal Reports Design
Using Cross-Tabs for Summarized Reporting
Using Record Selections and Alerts for Interactive Reporting
Using Subreports and Multi-Pass Reporting
Using Formulas and Custom Functions
Designing Effective Report Templates
Additional Data Sources for Crystal Reports
Multidimensional Reporting Against OLAP Data with Crystal Reports
Part IV. Enterprise Report Design Analytic, Web-based, and Excel Report Design
Introduction to Crystal Repository
Crystal Reports Semantic Layer Business Views
Creating Crystal Analysis Reports
Advanced Crystal Analysis Report Design
Ad-Hoc Application and Excel Plug-in for Ad-Hoc and Analytic Reporting
Part V. Web Report Distribution Using Crystal Enterprise
Introduction to Crystal Enterprise
Using Crystal Enterprise with Web Desktop
Crystal Enterprise Architecture
Planning Considerations When Deploying Crystal Enterprise
Deploying Crystal Enterprise in a Complex Network Environment
Administering and Configuring Crystal Enterprise
Part VI. Customized Report Distribution Using Crystal Reports Components
Java Reporting Components
Crystal Reports .NET Components
COM Reporting Components
Part VII. Customized Report Distribution Using Crystal Enterprise Embedded Edition
Introduction to Crystal Enterprise Embedded Edition
Crystal Enterprise Viewing Reports
Crystal Enterprise Embedded Report Modification and Creation
Part VIII. Customized Report Distribution Using Crystal Enterprise Professional
Introduction to the Crystal Enterprise Professional Object Model
Creating Enterprise Reports Applications with Crystal Enterprise Part I
Creating Enterprise Reporting Applications with Crystal Enterprise Part II
Appendix A. Using Sql Queries In Crystal Reports
Creating Enterprise Reporting Applications with Crystal Enterprise Part II