#### Grouping data dynamically

As described earlier you can make a multi-level group report by selecting a field as grouping criterion for the report. However, because the grouping criteria are definite, if you want to group several times according to different grouping criteria but based on the same table in the database it is cumbersome. For example, you want to make three employee list reports with different grouping criteria, the first one is grouped by their first name, the second one is by the hire date and the third one is by their salary, then you have to repeat the steps of setting query, selecting fields as grouping criteria, and so on, which is not efficient.

For this case you can use the Dynamic Grouping feature of JReport Designer, which means grouping criteria is a dynamic process. You don't need to repeat the same steps to make multiple reports with different grouping criteria. You can just predefine a parameter using String value type and add it to the group list. Then when you run the report, the parameter dialog prompts the end user to select a field to group by. All the acceptable group by fields are listed in a drop-down list. You can select any of them as grouping criterion.

##### Example of using dynamic grouping

Assume that you have created a table based on the EmployeeInformation query in the SampleReports.cat catalog, which displays the fields Name, Home Phone, Employee Position and Salary, and applies the default style. See the table.

To apply the dynamic grouping conditions, follow the steps below:

1. Right-click the table and select Table Wizard from the shortcut menu to display the Table Wizard.
2. In the Resources panel of Group screen, click <Add Parameter...> in the Parameters node.
3. In the Create Parameter dialog, create a type-in parameter named GroupBy of String type (leave the other settings to their default), then click OK.
4. Specify to group the report on the just created parameter GroupBy and use Ascend as the sorting order.
5. Click Finish in the Table Wizard to accept the settings.
6. Click the View tab to view the report. You will then be prompted to specify the parameter value. The parameter value drop-down list will show all available fields (including the DBFields in the query EmployeeInformation and formulas which are related to these DBFields). See illustration.

7. Select Employee Position from the value drop-down list and data in the table is then grouped by positions of the employees. See the result.

   To group the data by hire date or salary, the end user would need to select Hire Date or Salary from the drop-down list.