>
Avg Function
Description
Calculates the arithmetic mean of a set of values contained in a specified field on a query.
Syntax
Avg(expr)
The expr placeholder represents a string expression identifying the field that contains the numeric data you want to average or an expression that performs a calculation using the data in that field. Operands in expr can include the name of a table field, a constant, or a function (which can be either intrinsic or user-defined but not one of the other SQL aggregate or domain aggregate functions).
Remarks
The average calculated by Avg is the arithmetic mean (the sum of the values divided by the number of values). You could use Avg, for example, to calculate average freight cost.
The Avg function doesn't include any Null fields in the calculation.
You can use Avg in a query expression and in the SQL property of a QueryDef object or when creating a Recordset object based on an SQL query.
See Also
SQL Aggregate Functions.
Specifics (Microsoft Access)
In Microsoft Access, you can use the Avg function in the query design grid, in an SQL statement in SQL view of the Query window, or in an SQL statement within Visual Basic code. You can also use the Avg function in a calculated control on a form or report.
The Avg function is most useful in totals queries and crosstab queries. It functions the same way whether you create the query in the query design grid or as an SQL statement in SQL view.
In the query design grid, you can create a new totals query by clicking the Totals button on the toolbar. The Total row is then inserted in the grid. You can set the Total cell beneath a field to the aggregate function to perform on the data in that field.
For example, suppose you have an Orders table that has both a Freight field and a ShipCity field. You can create a query that displays the average freight cost for orders sent to each city to which your company ships. Create a new totals query, and drag the ShipCity field to the query design grid. The Total cell beneath the ShipCity field should be set to Group By. Drag the Freight field to the query design grid, and set the Total cell beneath it to Avg. When you run the query, it will display the average freight cost by city.
You can view the SQL statement for this query by switching to SQL view. In this example, Microsoft Access creates the following SQL statement.
SELECT DISTINCTROW Avg(Orders.Freight) AS AvgOfFreight, Orders.ShipCity
FROM Orders
GROUP BY Orders.ShipCity;
You can also use an SQL statement such as this one within Visual Basic code. For example, using the following code you can create a Recordset object based on the query defined by this SQL statement.
Dim dbs As DatabAse, rst As Recordset, strSQL As String
Set dbs = CurrentDb
strSQL = "SELECT DISTINCTROW Avg(Orders.Freight) AS AvgOfFreight, "_
"Orders.ShipCity FROM Orders GROUP BY Orders.ShipCity;"
Set rst = dbs.OpenRecordset(strSQL)
To use the Avg function in a calculated control, set the control's ControlSource property to an expression containing the Avg function. For example, to display the average freight cost for a set of orders in a text box, enter the following expression in the ControlSource property of the text box.
= Avg([Freight])
If you use the Avg function in a calculated control, you can restrict the set of records against which the function is performed by setting the form's Filter property.
Example
This example uses the Orders table to calculate the average freight charges for orders with freight charges over $100.
SELECT Avg(Freight) AS [Average Freight] FROM Orders WHERE Freight > 100;
Example (Microsoft Access)
The following example assumes that you have an Orders table that contains a field called Freight. You can use the Avg function to calculate the average freight charges for orders with freight charges over $100. Enter the following expression in SQL view in the Query window.
SELECT Avg([Freight]) AS [AverageFreight] FROM Orders WHERE [Freight] > 100;
The next example creates a calculated control that displays the average freight charges for all of the underlying records in a form based on the same Orders table. Open a new form and set its RecordSource property to Orders. Enter this expression in a the ControlSource property of a text box on the form. To apply a condition that limits the calculation to only some records, such as those for orders greater than $100, set the form's Filter property.
= Avg([Freight])