Filtering the fields of a query

You can specify certain criteria for the fields of a query to be retrieved from the database via a filter, so that when you build reports on the query, the returned results will be narrowed down. In addition, you can also apply sub-queries to a filter. You can type your filter criteria both in the format of a filter and a QBE (Query By Example). The overall conditions will include them both. Always click the SQL button to view your conditions and parse the query to ensure the syntax is valid.

Filtering with the QBE format

The criteria panel in the lower part of the Query Editor window is for you to filter out some unnecessary records in a query. This filter is in the format of Query by Example (QBE).

When filtering with the QBE format, you are prompted to type the search criteria into a template resembling the data record. The advantage of query-by-example retrieval is that you don't need to learn a query language to frame a query. All the data fields are shown to you, and all you need to do is to enter the information that restricts the search to the required criteria. Any fields left blank, however, will match everything.

The columns and menus allow you to specifically define criteria for the fields in JReport Designer. For example, if a field is labeled REGION, and this field is a list of all 50 states in the United States. If you only want to see information from California (CA) and New York (NY), in the criteria menu, you can pick out CA and NY by placing their names in the column as seen below.

You just need to type in NY and CA. JReport Designer automatically places the ='xx' (equal sign and quotes) in the column for you. Below is a list of the syntax available:

Note that in an Oracle database, if you want to use a Date or DateTime type parameter or a specific date or time to filter the field of a query, you need to apply the to_date() or to_timestamp() function in the filter condition, for example:

Filtering with the filter format

Compared with QBE, the filter provides you with more flexibility with entering your conditions. The expression includes not only the DBField selected, but also formulas and parameters. You can also manually type in strings that are supported by the database.

To filter with the filter format, select Filter from the Query menu. The Search Condition dialog appears. See the dialog.

Specifying expressions:

The expressions are the main section of the filter. To place a criterion, follow the steps below:

  1. Click the Add Condition button to add a condition line.
  2. In the field text box, specify the field to be filtered. You can either type in the name of the field manually or click to specify the field in the Expressions dialog.
  3. From the operator drop-down list, select an operator as required.
  4. In the value text box, click to specify the value of how to filter the field in the Expressions dialog or input the value manually. You can also use sub-queries to narrow down the result with the Subquery tab.
  5. If you want to add more conditions, click the Add Condition button, define the conditions and then set the relationship between the condition lines as required.
  6. When done, click OK to accept the settings.

See also Dynamically filtering queries for additional information about how parameters can be used to filter queries dynamically.

Tip: You can make use of the symbols at the bottom of the Expressions dialog to modify the expression according to your requirements.

Using sub-queries in filters

When filtering the fields of a query with the filter format, you are also enabled to use sub-queries to narrow down the result. The following are syntaxes that can be used in sub-queries.

Quantified predicate

A quantified predicate compares a value with a set of values.

EXISTS predicate

The EXISTS predicate tests for the existence of certain rows.

IN predicate

The IN predicate compares a value with a set of values.

The following example explains how to apply a subquery when filtering a field:

  1. Create a query named mainin in the catalog, add the table Customers and select the following columns: Customers_Customer ID, Customer Name, Customers_City, and Customers_Region.
  2. Click Filter on the Query menu to open the Search Condition dialog.
  3. Click the Add Condition button to add a condition line.
  4. Click beside the field text box. In the Expressions dialog, select the column Customers_Customer ID, then close the dialog.
  5. Select in as the operator from the operator drop-down list.
  6. Click beside the value text box. In the Expressions dialog, click the Subquery tab. Select an existing query in the catalog to be the subquery. If you want to edit the selected query, click the Edit Subquery button. To create a new subquery, click the New Subquery button.

    Here, we create a new query named subin, add the table Orders, select the column Orders_Customer ID, and add a condition "Ship Via=Express Delivery" in the Search Condition dialog.

  7. Click OK. The subquery subin will then be added into the value text box. Click OK to close the Search Condition dialog.

Now, the subquery subin will be applied to the filter when you build a report that uses the Customers_Customer ID column.

Notes: