SQL AVG 函数

时间:2018-10-17 12:24:03   收藏:0   阅读:1080

定义和用法

AVG 函数返回数值列的平均值。NULL 值不包括在计算中。

SQL AVG() 语法

SELECT AVG(column_name) FROM table_name

SQL AVG() 实例

我们拥有下面这个 "Orders" 表:

O_IdOrderDateOrderPriceCustomer
1 2008/12/29 1000 Bush
2 2008/11/23 1600 Carter
3 2008/10/05 700 Bush
4 2008/09/28 300 Bush
5 2008/08/06 2000 Adams
6 2008/07/21 100 Carter

例子 1

现在,我们希望计算 "OrderPrice" 字段的平均值。

我们使用如下 SQL 语句:

SELECT AVG(OrderPrice) AS OrderAverage FROM Orders

结果集类似这样:

OrderAverage
950

例子 2

现在,我们希望找到 OrderPrice 值高于 OrderPrice 平均值的客户。

我们使用如下 SQL 语句:

SELECT Customer FROM Orders
WHERE OrderPrice>(SELECT AVG(OrderPrice) FROM Orders)

结果集类似这样:

Customer
Bush
Carter
Adams
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!