标签:
AVG 函数返回数值列的平均值。NULL 值不包括在计算中。
SELECT AVG(column_name) FROM table_name
我们拥有下面这个 "Orders" 表:
| O_Id | OrderDate | OrderPrice | Customer |
|---|---|---|---|
| 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 |
现在,我们希望计算 "OrderPrice" 字段的平均值。
我们使用如下 SQL 语句:
SELECT AVG(OrderPrice) AS OrderAverage FROM Orders
结果集类似这样:
| OrderAverage |
|---|
| 950 |
现在,我们希望找到 OrderPrice 值高于 OrderPrice 平均值的客户。
我们使用如下 SQL 语句:
SELECT Customer FROM Orders WHERE OrderPrice>(SELECT AVG(OrderPrice) FROM Orders)
结果集类似这样:
| Customer |
|---|
| Bush |
| Carter |
| Adams |
COUNT() 函数返回匹配指定条件的行数。
COUNT(column_name) 函数返回指定列的值的数目(NULL 不计入):
SELECT COUNT(column_name) FROM table_name
COUNT(*) 函数返回表中的记录数:
SELECT COUNT(*) FROM table_name
COUNT(DISTINCT column_name) 函数返回指定列的不同值的数目:
SELECT COUNT(DISTINCT column_name) FROM table_name
注释:COUNT(DISTINCT) 适用于 ORACLE 和 Microsoft SQL Server,但是无法用于 Microsoft Access。
我们拥有下列 "Orders" 表:
| O_Id | OrderDate | OrderPrice | Customer |
|---|---|---|---|
| 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 |
现在,我们希望计算客户 "Carter" 的订单数。
我们使用如下 SQL 语句:
SELECT COUNT(Customer) AS CustomerNilsen FROM Orders WHERE Customer=‘Carter‘
以上 SQL 语句的结果是 2,因为客户 Carter 共有 2 个订单:
| CustomerNilsen |
|---|
| 2 |
如果我们省略 WHERE 子句,比如这样:
SELECT COUNT(*) AS NumberOfOrders FROM Orders
结果集类似这样:
| NumberOfOrders |
|---|
| 6 |
这是表中的总行数。
现在,我们希望计算 "Orders" 表中不同客户的数目。
我们使用如下 SQL 语句:
SELECT COUNT(DISTINCT Customer) AS NumberOfCustomers FROM Orders
结果集类似这样:
| NumberOfCustomers |
|---|
| 3 |
这是 "Orders" 表中不同客户(Bush, Carter 和 Adams)的数目。
FIRST() 函数返回指定的字段中第一个记录的值。
提示:可使用 ORDER BY 语句对记录进行排序。
SELECT FIRST(column_name) FROM table_name
我们拥有下面这个 "Orders" 表:
| O_Id | OrderDate | OrderPrice | Customer |
|---|---|---|---|
| 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 |
现在,我们希望查找 "OrderPrice" 列的第一个值。
我们使用如下 SQL 语句:
SELECT FIRST(OrderPrice) AS FirstOrderPrice FROM Orders
结果集类似这样:
| FirstOrderPrice |
|---|
| 1000 |
LAST() 函数返回指定的字段中最后一个记录的值。
提示:可使用 ORDER BY 语句对记录进行排序。
SELECT LAST(column_name) FROM table_name
我们拥有下面这个 "Orders" 表:
| O_Id | OrderDate | OrderPrice | Customer |
|---|---|---|---|
| 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 |
现在,我们希望查找 "OrderPrice" 列的最后一个值。
我们使用如下 SQL 语句:
SELECT LAST(OrderPrice) AS LastOrderPrice FROM Orders
结果集类似这样:
| LastOrderPrice |
|---|
| 100 |
MAX 函数返回一列中的最大值。NULL 值不包括在计算中。
SELECT MAX(column_name) FROM table_name
注释:MIN 和 MAX 也可用于文本列,以获得按字母顺序排列的最高或最低值。
我们拥有下面这个 "Orders" 表:
| O_Id | OrderDate | OrderPrice | Customer |
|---|---|---|---|
| 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 |
现在,我们希望查找 "OrderPrice" 列的最大值。
我们使用如下 SQL 语句:
SELECT MAX(OrderPrice) AS LargestOrderPrice FROM Orders
结果集类似这样:
| LargestOrderPrice |
|---|
| 2000 |
MIN 函数返回一列中的最小值。NULL 值不包括在计算中。
SELECT MIN(column_name) FROM table_name
注释:MIN 和 MAX 也可用于文本列,以获得按字母顺序排列的最高或最低值。
我们拥有下面这个 "Orders" 表:
| O_Id | OrderDate | OrderPrice | Customer |
|---|---|---|---|
| 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 |
现在,我们希望查找 "OrderPrice" 列的最小值。
我们使用如下 SQL 语句:
SELECT MIN(OrderPrice) AS SmallestOrderPrice FROM Orders
结果集类似这样:
| SmallestOrderPrice |
|---|
| 100 |
SUM 函数返回数值列的总数(总额)。
SELECT SUM(column_name) FROM table_name
我们拥有下面这个 "Orders" 表:
| O_Id | OrderDate | OrderPrice | Customer |
|---|---|---|---|
| 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 |
现在,我们希望查找 "OrderPrice" 字段的总数。
我们使用如下 SQL 语句:
SELECT SUM(OrderPrice) AS OrderTotal FROM Orders
结果集类似这样:
| OrderTotal |
|---|
| 5700 |
SQL--合计函数(Aggregate functions):avg,count,first,last,max,min,sum
标签:
原文地址:http://www.cnblogs.com/youguess/p/4595431.html