Creating Aggregate Functions in Access
Tutorial
The Tutorial #3 in the New Perspectives Access 2000 book has misleading
discussion of aggregate functions. In particular, the query will not run until
all three aggregate functions have been entered nor will it run as stated if
there are any other fields present in the query. The following is an attempt
at a clarified version of the tutorial.
Aggregate functions are operations on fields that calculate a single result
based on multiple records. A view that displays an aggregate function will,
in general, have fewer rows (records) than one that does not display an aggregate
function because the aggregate function has taken multiple records and processed
them to create a single record with the final result.
Examples of aggregate functions are
- The sum of all of a field's values in a table/query.
- The sum of all of a field's values for each unique value of another field.
- The average of all of a field's values in a table/query.
- The number of records in a table/query.
Making a query that shows the sum of all the invoice amounts:
- Open up your Restaurant database, or get the partially complete database
(right-click and save as: Restaurant2.mdb)
- Create a new query using the Design View, saving it under the name, "Statistics".
- Add both the Customer and Order table to the query builder.
- Drag the InvoiceAmt field down to the design grid.
- Click the "Totals" button on the top toolbar (it has a capital
Greek "sigma" character on it). You should see a new row appear
in the design grid, called "Total:". The Totals row should say "Group
By" in it.
- Run the query. You should see a table with many rows of numbers in it.
- Now pull the drop-list down by clicking the right side of the Totals text
box and select "Sum".
- Re-run the query. You should see only one row with the total of all the
invoice amounts.
Here are some things to try:
Sum of all the paid and unpaid invoices:
- To the above query, add the Paid field.
- Run the query. You should see two rows, one with Paid checked and one with
it unchecked. The checked Paid row has the sum of all the paid invoices. The
unchecked Paid row has the sum of all the unpaid invoices.
Sum of all the unpaid invoices:
- To the previous query above, add the criteria "=false" to the
Paid field in the Design grid.
- Run the query. You should see only one row now with the sum of all the unpaid
queries.
Sum of all the unpaid invoices for each customer:
- Add a Customer field to the previous query above.
- Run the query. You should see multiple rows, each with a unique customer
and the sum of all that customer's unpaid invoices.
Sum of all the unpaid invoices for each customer and the count of how many
unpaid invoices there were for that customer:
- Add another InvoiceAmt field to the Design grid.
- Set that field's Totals text box to be "Count".
- Run the query. You should see the same output as the previous query, but
with an additional field that tells you how many unpaid invoices that customer
has (that were summed together).
The database at this point can be found here: right-click and save as: Restaurant3.mdb.