### 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:

- Comparison predicates ( =, >, <, >=, <=, <> ).
- BETWEEN predicate (example: BETWEEN 1 AND 100).
- IN predicate (example: NOT IN (1, 3, 5)).
- LIKE predicate (example: LIKE '%apple%').
- NULL predicate (example: IS NOT NULL).

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.

- Syntax
  `expression----+- = --+- SOME --+-- ( subselect )
  +- <> -+ ANY ---+
  +- ! = --+ ALL ----+
  +- < --+
  +- > --+
  +- <= --+
  +- ! > --+
  +- >= --+
  +- ! < --+`

  The subselect must specify a single result column and can return any number of values, whether they are null or not.

  - When ALL is specified, the result of the predicate is:
    - True if the result of the subselect is empty, or if the specified relationship is true for every value returned by the subselect.
    - False if the specified relationship is false for at least one value returned by the subselect.
    - Unknown if the specified relationship is not false for any values returned by the subselect and at least one comparison is unknown because of a null value.
  - When SOME or ANY is specified, the result of the predicate is:
    - True if the specified relationship is true for at least one value returned by the subselect.
    - False if the result of the subselect is empty, or if the specified relationship is false for every value returned by the subselect.
    - Unknown if the specified relationship is not true for any of the values returned by the subselect and at least one comparison is unknown because of a null value.
- Examples
  `SELECT qty FROM sales WHERE qty>= ALL (SELECT qty FROM sales)
  SELECT BUYERID, ITEM FROM ANTIQUES WHERE PRICE != ANY (SELECT PRICE FROM ANTIQUES);`

EXISTS predicate

The EXISTS predicate tests for the existence of certain rows.

- Syntax
  `- [ NOT ] EXISTS--(subselect)`

  The subselect may specify any number of columns and,

  - The result is true only if the number of rows specified by the subselect is not zero.
  - The result is false only if the number of rows specified by the subselect is zero.
  - The result cannot be unknown.
- Example
  `SELECT DISTINCT pub_name FROM publishers WHERE EXISTS (SELECT * FROM titles WHERE pub_id = publishers.pub_id AND type = 'business')`

IN predicate

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

- Syntax
  `expression----+-- [NOT] IN --+-- ( subselect )`

  In the subselect form, the subselect must identify a single result column and may return any number of values, whether null or not null.

- Example
  `SELECT distinct pub_name FROM publishers WHERE pub_id IN (SELECT pub_id FROM titles WHERE type = 'business')`

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:

- If a query uses a parameter in its search criteria, you can decide what the query does in case that the parameter value is NULL at runtime (for example, not provided). You can choose to remove the parameter condition from the query's criterion, or treat the parameter value as a default value (0) or an empty string, which can cause great difference in your report result.

  To remove the parameter condition from a query if this happens, check Ignore Predicate If Parameter Value Is Null on the Query menu.

- For string type parameters, when the value is left blank, if the Ignore Predicate If Parameter Value Is Null is checked, the value of this parameter will be considered as NULL, and this predicate will not appear in the where clause; if not checked, it will be treated as an empty string ("").
- The following SQL type of data cannot be sorted: Db.SQL_BINARY, Db.SQL_BLOB, Db.SQL_CLOB, Db.SQL_LONGVARCHAR, Db.SQL_LONGVARBINARY, Db.SQL_VARBINARY and Db.SQL_OTHER.