GROUP BY子句
Posted jly1
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GROUP BY子句相关的知识,希望对你有一定的参考价值。
GROUP BY 常用的聚合函数, 可以对数据进行汇总
- 一般与常见的一些函数一起使用, 汇总数据后一般没有办法进行使用
- 使用顺序, SELECT, FROM, JOIN, WHERE GROUP BY, HAVING, ORDER BY, LIMIT
-- 使用聚合函数
-- MAX()
-- MIN()
-- AVG()
-- SUM()
-- COUNT();
SELECT
MAX(invoice_total) AS highest,
MIN(invoice_total) AS lowest,
AVG(invoice_total) AS average,
SUM(invoice_total) AS total,
SUM(invoice_total * 1.1) AS number_of_invoices,
COUNT(payment_date) AS count_of_payments,
COUNT(*) AS total_records
FROM invoices
WHERE invoice_date > ‘2019-07-01‘;
USE sql_invoicing;
SELECT
MAX(invoice_total) AS highest,
MIN(invoice_total) AS lowest,
AVG(invoice_total) AS average,
SUM(invoice_total) AS total,
COUNT(invoice_total * 1.1) AS number_of_invoices,
COUNT(payment_date) AS count_of_payments,
COUNT(DISTINCT client_id) AS total_records
FROM invoices
WHERE invoice_date > ‘2019-07-01‘;
SELECT
SUM(invoice_total) AS total_sales,
SUM(payment_total) AS total_payments,
SUM(invoice_total) - SUM(payment_total) AS what_we_expect
FROM invoices;
-- Exercise
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-30‘
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-30‘;
-- 数据分组
SELECT
state,
city,
client_id,
SUM(invoice_total) AS total_sales
FROM invoices i
JOIN clients USING (client_id)
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)
WHERE invoice_date
GROUP BY state, city;
SELECT
date,
pm.name AS payment_method,
SUM(amount) AS total_payments
FROM payments p
JOIN payment_methods pm
ON p.payment_method = pm.payment_method_id
GROUP BY date, payment_method
ORDER BY date;
-- HAVING
SELECT
client_id,
SUM(invoice_total) AS total_sales,
COUNT(*) AS number_of_invoices
FROM invoices
GROUP BY client_id
HAVING total_sales > 500;
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;
-- Exercise
-- Get the customers
-- located in Virginia
-- Who have spent more than 100
USE sql_store;
SELECT
order_id,
first_name,
last_name,
state,
SUM(quantity * unit_price) AS total_price
FROM customers c
JOIN orders o
USING (customer_id)
LEFT JOIN order_items oi
USING (order_id)
GROUP BY order_id
HAVING state = ‘VA‘ and total_price > 60;
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 state = ‘VA‘
GROUP BY
c.customer_id,
c.first_name,
c.last_name
HAVING total_sales > 100;
-- WITH ROLLUP
USE sql_invoicing;
SELECT
client_id,
SUM(invoice_total) AS total_sales
FROM invoices
GROUP BY client_id WITH ROLLUP;
-- 分组,只能在mysql用
SELECT
state,
city,
SUM(invoice_total) AS total_sales
FROM invoices i
JOIN clients c USING (client_id)
GROUP BY state, city WITH ROLLUP;
-- Exercise
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;
SELECT
pm.name AS payment_method,
SUM(amount) AS total
FROM payments p
JOIN payment_methods pm
ON p.payment_method = pm.payment_method_id
GROUP BY pm.name WITH ROLLUP;
以上是关于GROUP BY子句的主要内容,如果未能解决你的问题,请参考以下文章
我们如何在具有GROUP BY子句的查询中选择非聚合列,而GROUP BY子句在功能上不依赖于GROUP BY子句中的列?