Microsoft Access SQL) (Avg 函数

适用于:Access 2013 | Access 2016

计算查询的指定字段中所包含的一组值的算术平均值。

语法

Avg(expr)

expr 占位符代表一个字符串表达式(它标识了包含您要求平均值的数字数据的字段),或者代表一个使用该字段中的数据执行计算的表达式。 expr 中的操作数可以包括表字段、常量或者函数(可以是固有函数或用户定义的函数,但不能是其他 SQL 聚合函数)的名称。

备注

使用 Avg 计算的平均值是算术平均值(值的总和除以值的数目)。 例如,可以使用 Avg 计算运费的平均值。

在计算中,Avg 函数不能包含任何 Null 字段。

在查询表达式和 QueryDef 对象的 SQL 属性中使用 Avg,或者在基于 SQL 查询创建 Recordset 对象时使用 Avg

示例

以下示例使用 Orders 表计算运货费超过 ¥100 的订单的平均运货费。

以下示例调用 EnumFields 过程,您可以在 SELECT 语句示例中找到该过程。

Sub AvgX() 
 
    Dim dbs As Database, rst As Recordset 
 
    ' Modify this line to include the path to Northwind 
    ' on your computer. 
    Set dbs = OpenDatabase("Northwind.mdb") 
 
    ' Calculate the average freight charges for orders 
    ' with freight charges over $100.   
    Set rst = dbs.OpenRecordset("SELECT Avg(Freight)" _ 
        & " AS [Average Freight]" _ 
        & " FROM Orders WHERE Freight > 100;") 
    
    ' Populate the Recordset. 
    rst.MoveLast 
    
    ' Call EnumFields to print the contents of the  
    ' Recordset. Pass the Recordset object and desired 
    ' field width. 
    EnumFields rst, 25 
 
    dbs.Close 
 
End Sub

另请参阅

支持和反馈

有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。