SQL-W3School-函数:SQL AVG() 函数

ylbtech-SQL-W3School-函数:SQL AVG() 函数
1.返回顶部
1、

定义和用法

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
2、
2.返回顶部
 
3.返回顶部
 
4.返回顶部
 
5.返回顶部
1、
2、
 
6.返回顶部
 
warn 作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/storebook/p/11827548.html