Strategies for writing efficient record selection formulas

Consider the following points when creating a record selection formula:

Consideration 1

Any record selection formula that you generate completely with the Select Expert, without writing pieces of the formula yourself, can be pushed down.

This case actually follows from the points below. However, you can write more types of record selection formulas using the tips below than is possible with the Select Expert. To do this, you need to edit the record selection formula directly with the Formula Workshop or from within the text area that appears when you click Show Formula in the Select Expert.

To open the Formula Workshop to modify record selection, click the Report menu, point to Selection Formulas, and then select Record from the submenu.

Consideration 2

To open the Formula Workshop to modify record selection, click the Report menu, point to Selection Formulas, and then select Record from the submenu.

Any selection formula that is of the form: DataBaseField SupportedOperator ConstantOrParameterExpression can be pushed down.

Of course DataBaseField is just a database field. SupportedOperator is any of =, <>, <, <=, >, >=, StartsWith, Like or In.

ConstantOrParameterExpression is any expression that involves constant values, operators, functions, and parameter fields. It cannot involve variables, control structures, or fields other than parameter fields. By their very definition, constant and parameter expressions can be evaluated without accessing the database.
{?number parameter} - 3
Year ({?run date})
CurrentDate + 5
DateDiff ("q", CurrentDate, CDate("Jan 1, 1996"))
Month (Maximum ({?date range parameter}) + 15)
["Canada", "Mexico", "USA", {?enter a country}]
1000 To 5000
[5000 To 10000, 20000 To 30000, 50000 To 60000]

A complete example:

{Orders.Order Date} >= CurrentDate - 3

The program can also push down an expression that just contains a Boolean field (without the operator and constant parts).

{Orders.Shipped}
Not {Orders.Shipped}

Consideration 3

IsNull ( <DataBaseField> ) can be pushed down.

Consideration 4

<SqlExpression SupportedOperator ConstantOrParameterExpression> can be pushed down.

For example, the selection formula {@ExtendedPrice} > 1000 cannot be pushed down if {@ExtendedPrice} = (Quantity * Price). However, if the formula @ExtendedPrice is replaced with the equivalent SQL Expression, then your record selection formula will be pushed down.

Consideration 5

When using multiple expressions that follow the above considerations, separate them with AND and OR operators—you can also use NOT. You can have several of each, and you can use parentheses to give priority. For example:

{Orders.Order ID} < Minimum({?number range}) Or
{Orders.Order Amount} >= 1000
(IsNull({Customer.Region}) Or
{Customer.Region} = "BC") And
{Customer.Last Year's Sales} > 2000

To add a parameter field to your record selection formula

  1. Open the sample report Group By Intervals.rpt.
    Take a moment to navigate the Group tree and see how the data is organized; notice also, in the bottom right of the Crystal Reports window, that 269 records were returned for this report.

    Now click the Design tab.

  2. On the View menu, click Field Explorer.
  3. In the Field Explorer, right-click Parameter Fields and select New from the shortcut menu.

    The Create New Parameter dialog box appears.

  4. For the Name of the parameter, type SalesQuota.
  5. Click the Type list and select Number.
  6. In the Options area, go to the Prompt Text option and type What was last year's sales quota?
  7. In the Options area, make sure that the Allow discrete values option is True, and then click OK.
  8. On the Report menu, click Select Expert.

    The Choose Field dialog box appears.

  9. Select the Customer.Last Year's Sales field, and then click OK.

    The Select Expert appears.

  10. On the Customer.Last Year's Sales tab, click the drop-down list and select is greater than.

    A new drop-down list appears.

  11. Click this second list and select {?SalesQuota}.
  12. Click OK in the Select Expert.

You've now added your parameter field to the record selection formula. When you switch to Preview mode, or click the Refresh button, you will be prompted for new parameter values. You can then enter a numeric value representing last year's sales quota. The resulting report will display only those customers whose Last Year's Sales exceeds the numeric value you specify.

For instance, if you respond to the parameter by entering 40000, then the report will display only those customers whose sales exceeded $40,000 last year. Notice also, in the bottom right of the Crystal Reports window, that only 58 records are returned for your report, instead of the 270 records that were returned before you put the parameter field into a record selection formula.

By enhancing this report, you've retrieved all the information you needed and, at the same time, you've ensured that the fewest number of records is transferred from the database server.