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 num
FROM invoices
-- 一个没有空行,一个有空行,结果不同
SELECT MAX(payment_date) AS latest,
COUNT(invoice_total) AS num,
COUNT(payment_date) AS count_of_payments
COUNT(*) AS total_records
FROM invoices
SELECT MAX(invoice_total) AS highest,
MIN(invoice_total) AS lowest,
AVG(invoice_total) AS average,
SUM(invoice_total * 1.1) AS total,
-- client_id中有重复的,结果7
COUNT(client_id) AS num
-- 可用distinc去重,结果3
COUNT(DISTINCT client_id) AS num
FROM invoices
WHERE invoice_date > '2019-07-01';
SELECT
'First half of 2019' AS date_range,
SUM(invoice_total) AS total_sales,
SUM(payment_total) AS total_payments,
SUM(invoice_total - payment_total) AS what_we_expect
FROM invoices
WHERE invoice_date BETWEEN '2019-01-01' AND '2019-06-30'
UNION
SELECT
'Second half of 2019' AS date_range,
SUM(invoice_total) AS total_sales,
SUM(payment_total) AS total_payments,
SUM(invoice_total - payment_total) AS what_we_expect
FROM invoices
WHERE invoice_date BETWEEN '2019-07-01' AND '2019-12-31'
UNION
SELECT
'Total' AS date_range,
SUM(invoice_total) AS total_sales,
SUM(payment_total) AS total_payments,
SUM(invoice_total - payment_total) AS what_we_expect
FROM invoices
WHERE invoice_date BETWEEN '2019-01-01' AND '2019-12-31'
把sum按照client_id分组
还可以排序
还可以添加筛选条件
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
state,
city,
sum(invoice_total) as total_sales
-- 连接两个表
from invoices i
JOIN clients using (client_id)
-- 每个state和city的组合
group by state, city
-- 按支付日期、支付方式分组计算payment_total的总值
select p.date,
pm.name,
sum(payment_total) as 'total_payments'
from invoices i
join payments p using (invoice_id)
join payment_methods pm on p.payment_method = pm.payment_method_id
group by p.date, payment_method
-- 按clientid把totalsales分组后,想获得total大于500的客户。怎么办呢?
-- 此时不能在from后面用where totalsales> 500,因为此时total_sales的结果还没有
select client_id,
sum(invoice_total) as total_sales
from invoices
group by client_id
select client_id,
sum(invoice_total) as total_sales
from invoices
group by client_id
-- 在group by后用having,此时把大于500的筛选出来了
having total_sales > 500
select client_id,
sum(invoice_total) as total_sales,
count(*) as number_of_invoices
from invoices
group by client_id
-- 想筛选total_sales大于500且发票数量大于5的,用and连接
having total_sales > 500 and number_of_invoices > 5
having子句中筛选的列,一定是在select中出现的。而where则没有这样的限制。
练习
找到位于VA的,消费总额大于100的顾客
use sql_store;
select customer_id,
sum(unit_price * quantity) as total_price
from customers c
join orders o using (customer_id)
join order_items using (order_id)
where state = 'VA'
group by customer_id
having total_ price > 100
select client_id,
sum(invoice_total) as total_sales
from invoices
group by client_id with rollup
select state,
city,
sum(invoice_total) as total_sales
from invoices i
join clients c using (client_id)
group by state, city with rollup
运行结果
use sql_invoicing;
select pm.name,
sum(amount) as total
from payments p
join payment_methods pm
on payment_method_id = payment_method
group by name with rollup