Export (0) Print
Expand All

AVG

SQL Server 2000

Returns the average of the values in a group. Null values are ignored.

Syntax
AVG ( [ ALL ] expression ) 
Arguments
ALL
Applies the aggregate function to all values. ALL is the default.
expression
Is an expression of the exact numeric or approximate numeric data type categories, except for the bit data type. Aggregate functions and subqueries are not permitted.
Return Types

The return type is determined by the type of the evaluated result of expression.

Expression result Return type
integer category int
numeric category (p, s) numeric(28, s) divided by numeric(10, 0)
money category money
float and real category float
Examples

The following example uses the Orders table in the Northwind database. The example determines the average freight shipped to a company that has a CustomerID of VINET in 1996.

--Get all OrderIDs for the VINET CustomerID.
SELECT OrderID, ShippedDate FROM Orders WHERE CustomerID = 'VINET'

This is the result set:

OrderID     ShippedDate           
----------------------------------
10248      1996-07-16 00:00:00.000
10274      1996-08-16 00:00:00.000
10295      1996-09-10 00:00:00.000
10737      1997-11-18 00:00:00.000
10739      1997-11-17 00:00:00.000

--Get the average freight for all orders shipped in 1996.
SELECT AVG(Freight) AS "Average 1996 Freight" FROM Orders WHERE CustomerID = 'VINET' AND OrderID IN (10248, 10274, 10295)

This is the result set:

Average 1996 Freight
--------------------
13.18

Show:
© 2014 Microsoft