聚合函数:输入一系列值并聚合为一个结果的函数。
SELECT
MAX(invoice_total) AS highest,
MIN(invoice_total) AS lowest, -- 函数用于属性栏定义中
AVG(invoice_total) AS average,
SUM(invoice_total) AS total,
COUNT(invoice_total) AS number_of_invioces,
COUNT(*) AS total_records, -- 表中共有多少条记录
COUNT(DISTINCT client_id) AS number_of_clients
-- DISTINCT 用于删除重复的属性值,然后再计数
FROM invoices
WHERE invoice_date > '2019-07-01'
按一列或多列中记录利用某种规则重新汇总,生成新的记录。
语法:GROUP BY …… 用于将某个属性列下相同的值进行合并,一般和函数公用,函数放在SELECT 后,GROUP BY放在WHERE后
NOTE:
SELECT
client_id,
SUM(invoice_total) AS total_sales
FROM invoices
WHERE invoice_date > '2019-07-01'
GROUP BY client_id
ORDER BY total_sales DESC
SELECT
c.state,
c.city,
SUM(i.invoice_total) AS total_sales
-- SELECT通常都是选择分组依据列+目标统计列的聚合函数。选别的列没有意义
FROM invoices i
JOIN clients c
USING (client_id)
WHERE invoice_date > '2019-07-01'
GROUP BY c.state, c.city
-- 虽然用了两个属性值为分组依据,但是从数据来说仍然是一个城市的
SELECT
p.date,
pm.name,
SUM(amount) AS total_payments
FROM payments p
JOIN payment_methods pm
ON P.payment_method = pm.payment_method_id
GROUP BY p.date, p.payment_method
ORDER BY p.date
-- 用了两个属性值为统计依据,再满足第一个汇总依据基础上再满足第二个汇总依据
HAVING 和 WHERE 都是是条件筛选语句,条件的写法相通,数学比较(包括特殊比较)逻辑运算都可以用(如AND、REGEXP等等)。
两者本质区别:
SELECT
client_id,
SUM(invoice_total) AS total_sales,
COUNT(*) AS number_of_invoices
FROM invoices
GROUP BY client_id
HAVING total_sales > 500 AND number_of_invoices >5
-- HAVING子句用于统计后的数据筛选
SELECT
c.customer_id,
c.first_name,
c.last_name,
SUM(oi.quantity * oi.unit_price) AS total_sales
FROM customers c
JOIN orders o USING (customer_id)
JOIN order_items oi USING (order_id)
WHERE c.state = 'VA'
GROUP BY c.customer_id
HAVING total_sales > 100
NOTE: 当HAVING筛选的是聚合函数时,该聚合函数可以不在SELECT里显性出现。
GROUP BY …… WITH ROLLUP 自动汇总型分组(对 SUM 之类的聚合值进行分组后的汇总),若是多字段分组的话汇总也会是多层次的,注意这是MySQL扩展语法,不是SQL标准语法。
WITH ROLLUP前的属性名一定要在SELECT中呈现出来,如果只是GROUP BY就不一定要在SELECT中表现出来。
SELECT
pm.name AS payment_method,
SUM(p.amount) AS total
FROM payments p
JOIN payment_methods pm
ON p.payment_method = pm.payment_method_id
GROUP BY pm.name WITH ROLLUP