Performing Calculations in a Query


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:

Function

Calculates

Sum

Total of the values in a field

Avg

Average of the values in a field

Count

Number of values in a field, not counting Null (blank) values

Min

Lowest value in a field

Max

Highest value in a field

StDev

Standard deviation of the values in a field

Var

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.

  1. 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.

  2. 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.

  3. In the list of fields in the Products table, double-click ProductID and then UnitPrice .

    Access moves both fields to the design grid.

  4. Click the Totals button on the toolbar.

    A row named Total is added to the design grid.

    click to expand
  5. 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.

  6. 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.

  7. Click the Run button.

    The result of the query is a single record containing the count and the average price.

    click to expand
  8. Click the View button to return to Design view.

  9. 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.

  10. Set the Total cell of the third column to Sum to return the sum of all the values calculated by the expression.

  11. Select Expr1: , and type Value of Inventory: .

  12. Run the query again.

    click to expand
  13. Close the query window, clicking No when prompted to save the query.

CLOSE the GardenCo database.




Microsoft Office 2003 Step by Step
MicrosoftВ® Office ExcelВ® 2003 Step by Step (Step By Step (Microsoft))
ISBN: 0735615187
EAN: 2147483647
Year: 2005
Pages: 350
Authors: Curtis Frye

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net