Search
Office

Microsoft Access 2010 : Enhancing the Queries That You Build - Creating Totals Queries

Published 2 min readBy Programming4us Editorial Team

To create a Totals query, follow these steps:

1.

Add to the query grid the fields or expressions you want to summarize. It’s important that you add the fields in the order in which you want them grouped. For example, Figure 1 shows a query grouped by country and then by city.

Figure 1. Selecting from a drop-down list the type of calculation for the Total row.


2.

Click Totals in the Show/Hide group on the Design tab of the Ribbon to add a Total row to the query. By default, each field in the query has Group By in the Total row.

3.

Click the Total row on the design grid.

4.

Open the combo box and choose the calculation you want, as shown in Figure 1.

5.

Leave Group By in the Total row for any field you want to group by, as shown in Figure 1. Remember to place the fields in the order in which you want them grouped. For example, if you want the records grouped by country and then by sales representative, you must place the Country field to the left of the Sales Representative field on the query grid. On the other hand, if you want records grouped by sales representative and then by country, you must place the Sales Representative field to the left of the Country field on the query grid.

6.

Add to the query the criteria you want.

Figure 2 shows the design of a query that finds the total, minimum, maximum, and average sales by country and city; Figure 3 shows the results of running the query. As you can see, aggregate functions can give you valuable information.

Figure 2. A query that finds the total, minimum, maximum, and average sales by country and city.


Figure 3. The result of running a query that has many aggregate functions.


If you save this query and reopen it, you should see that Access has made some changes to its design. Access changes the Total cell for Sum to Expression, and it changes the Field cell to the following:

Total: Sum([Unit Price]*[Quantity])

If you look at the Total cell for Avg, you should see that Access changes it to Expression. Access changes the Field cell to the following:

Average: Avg([Unit Price]*[Quantity])

Access modifies the query in this way when it determines that you’re using an aggregate function on an expression that has more than one field. You can enter the expression either way. Access stores and resolves the expression as noted.