You typically use a query to locate all the records that meet some criteria. But sometimes you are not as interested in the details of all the records as you are in summarizing them in some way. As an example, you might want to know how many orders have been placed this year or the total dollar value of all orders placed. The easiest way to get this information is to create a query that groups the necessary fields and does the math for you. To do this, you use aggregate functions in the query.
Access queries support the following aggregate functions:
Total of the values in a field
Average of the values in a field
Number of values in a field, not counting Null (blank) values
Lowest value in a field
Highest value in a field
Standard deviation of the values in a field
Variance of the values in a field
In this exercise, you will create a query that calculates the total number of products in The Garden Company s inventory, the average price of all the products, and the total value of the inventory.
USE the GardenCo database in the practice file folder for this topic. This practice file is located in the My Documents\Microsoft Press\Office 2003 SBS\Queries\Aggregate folder and can also be accessed by clicking Start/All Programs/Microsoft Press/Microsoft Office System 2003 Step by Step .
OPEN the GardenCo database and acknowledge the safety warning, if necessary.
On the Objects bar, click Queries , and then double-click Create query in Design view .
Access first opens the query window in Design view and then displays the Show Table dialog box.
In the Show Table dialog box, double-click Products , and click Close .
Access adds the Products table to the query window and closes the Show Table dialog box.
In the list of fields in the Products table, double-click ProductID and then UnitPrice .
Access moves both fields to the design grid.
Click the Totals button on the toolbar.
A row named Total is added to the design grid.
Click in the Total cell of the ProductID column, click the down arrow, and click Count in the drop-down list.
Access enters the word Count in the Total cell. When you run the query, this function will return a count of the number of records containing a value in the ProductID field.
In the UnitPrice column, set the Total cell to Avg .
When you run the query, this function will return the average of all the UnitPrice values.
Click the Run button.
The result of the query is a single record containing the count and the average price.
Click the View button to return to Design view.
In the Field cell of the third column, type UnitPrice*UnitsInStock and press [ENTER].
The text you typed is changed to Expr1: [UnitPrice]*[UnitsInStock] . This expression will multiply the price of each product by the number of units in stock.
Set the Total cell of the third column to Sum to return the sum of all the values calculated by the expression.
Select Expr1: , and type Value of Inventory: .
Run the query again.
Close the query window, clicking No when prompted to save the query.
CLOSE the GardenCo database.