Sams Teach Yourself SQL in 10 Minutes(Fourth Edition)学习记录

第九课 概括数据(总结数据)

本课学习什么是SQL聚合函数,以及如何使用这些函数来总结概括表数据。

9.1 使用聚合函数(Using Aggregate Functions)

1、确定表中行的数量(或者满足某些条件或包含一个特定值的行的数量)
2、获取表中一组行的总和。
3、找到一个表列的(或者是所有行的,或者是某些特定行的)最大值、最小值和平均值。

聚合函数(Aggregate Functions)

在一组行上操作来计算机或返回单子值的函数。

Table 9.1. SQL Aggregate Functions


image.png
9.1.1 The AVG() Function

AVG()用于通过计算表中所有行数及其值的总和来返回一个特定列的平均值。

输入:

SELECT AVG(prod_price) AS avg_price
FROM Products;

输入:

SELECT AVG(prod_price) AS avg_price
FROM Products
WHERE vend_id = 'DLL01';
9.1.2 The COUNT() Function

1、使用COUNT(*) 计算表中行的数量,不论列包含值或者NULL值。
2、使用COUNT(column)计算行的数量,特定列有值,忽略NULL值。

输入:

SELECT COUNT(*) AS num_cust
FROM Customers;

COUNT(*)用于计算所有行,不考虑其值,返回给num_cust。

输入:

SELECT COUNT(cust_email) AS num_cust
FROM Customers;

COUNT(cust_email) 计算在cust_email列中有值行数。

Note: NULL Values
Column rows with NULL values in them are ignored by the COUNT() function if a column name is specified, but not if the asterisk (*) is used.
9.1.3 The MAX()函数

输入:

SELECT MAX(prod_price) AS max_price
FROM Products;
9.1.4 The MIN()函数

输入:

SELECT MIN(prod_price) AS min_price
FROM Products;
9.1.5 The SUM()函数

输入:

SELECT SUM(quantity) AS items_ordered
FROM OrderItems
WHERE order_num = 20005;

输入:

SELECT SUM(item_price*quantity) AS total_price
FROM OrderItems
WHERE order_num = 20005;

9.2 有区别的值的集合(Aggregates on Distinct Values)

输入:

SELECT AVG(DISTINCT prod_price) AS avg_price
FROM Products
WHERE vend_id = 'DLL01';

Caution: No DISTINCT With COUNT(*)

DISTINCT may only be used with COUNT() if a column name is specified. DISTINCT may not be used with COUNT(*). Similarly, DISTINCT must be used with a column name and not with a calculation or expression.

Tip: Using DISTINCT with MIN() and MAX()

Although DISTINCT can technically be used with MIN() and MAX(), there is actually no value in doing so. The minimum and maximum values in a column will be the same whether or not only distinct values are included.

Note: Additional Aggregate Arguments

In addition to the DISTINCT and ALL arguments shown here, some DBMSs support additional arguments such as TOP and TOP PERCENT that let you perform calculations on subsets of query results. Refer to your DBMS documentation to determine exactly what arguments are available to you.

9.3 组合聚合函数(Combining Aggregate Functions)

输入:

SELECT COUNT(*) AS num_items,
MIN(prod_price) AS price_min,
MAX(prod_price) AS price_max,
AVG(prod_price) AS price_avg
FROM Products;

Caution: Naming Aliases

When specifying alias names to contain the results of an aggregate function, try to not use the name of an actual column in the table. Although there is nothing actually illegal about doing so, many SQL implementations do not support this and will generate obscure error messages if you do so.

9.4 总结

Aggregate functions are used to summarize data. SQL supports five aggregate functions, all of which can be used in multiple ways to return just the results you need. These functions are designed to be highly efficient, and they usually return results far more quickly than you could calculate them yourself within your own client application.

你可能感兴趣的:(Sams Teach Yourself SQL in 10 Minutes(Fourth Edition)学习记录)