Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

10.19 Grouping and Sorting

Grouping and Sorting Records

The two SQL statements that control grouping and sorting are GROUP BY and ORDER BY. Grouping records with the GROUP BY statement is pretty easy. All you do is list the applicable fields after each keyword. For example, if you want to group by the Customer ID, then you would use:

GROUP BY CustomerId

Using the ORDER BY keyword works the same way. There is one minor addition in that you can also specify the sort order. By default, all fields are sorted in ascending order. To sort in ascending order, just list the field name without doing anything else. If you want to sort in descending order, you need to use the DESC keyword. The following example shows what the previous SQL statement looks like if we sort it by the region in descending order.



Figure 10-13. SQL query for ordering by the Region field in descending order.