2021-07-26 SQL Day8

一.聚合函数

1.计数函数 COUNT( )

image.png
SELECT COUNT(1) FROM milk_tea AS m;

SELECT COUNT(m.prod_name) FROM milk_tea AS m;

SELECT COUNT(m.pro_date) FROM milk_tea AS m;

SELECT COUNT(m.sale_price) FROM milk_tea AS m;

SELECT COUNT(DISTINCT m.sale_price), COUNT(DISTINCT m.pro_date) FROM milk_tea AS m;

SELECT DISTINCT m.sale_price FROM milk_tea AS m;
image.png
image.png

2.求和函数 SUM

SELECT SUM(m.in_price) FROM milk_tea AS m;  ---78.5

SELECT SUM(m.in_price * 1.1) FROM milk_tea AS m;  ---86.35

SELECT SUM(m.sale_price) FROM milk_tea AS m;  ---94.5

SELECT SUM(m.sale_price - m.in_price) FROM milk_tea AS m;  ---31.5

SELECT SUM(IFNULL(m.sale_price, 0) - m.in_price) FROM milk_tea AS m;  ---16
image.png
SELECT SUM(m.sale_price) FROM milk_tea AS m WHERE m.net_w = '100g';  ---34

SELECT SUM(m.sale_price) FROM milk_tea AS m WHERE m.net_w = '150g';  ---35
image.png

3.均值函数 AVG

SELECT AVG(m.sale_price) FROM milk_tea AS m;  ---13.5

SELECT SUM(m.sale_price)/COUNT(m.sale_price) FROM milk_tea AS m;  ---13.5

SELECT SUM(m.sale_price)/COUNT(1) FROM milk_tea AS m;  ---11.8125
image.png
image.png

4.最大值函数 MAX,最小值函数 MIN

SELECT MAX(m.sale_price) FROM milk_tea AS m;

SELECT MIN(m.sale_price) FROM milk_tea AS m;
image.png
SELECT COUNT(m.sale_price), SUM(m.sale_price), AVG(m.sale_price), MIN(m.sale_price), MAX(m.sale_price) FROM milk_tea AS m;
image.png
image.png

二.分组过滤

1.GROUP BY

SELECT m.net_w, SUM(m.sale_price) FROM milk_tea AS m GROUP BY m.net_w;

SELECT m.net_w, SUM(m.sale_price) FROM milk_tea AS m WHERE m.net_w IN ('100g', '150g') GROUP BY m.net_w;

SELECT m.net_w, COUNT(m.sale_price) FROM milk_tea AS m WHERE m.net_w IN('100g', '150g') GROUP BY m.net_w;
image.png
image.png

2.HAVING

SELECT m.net_w, SUM(m.sale_price) FROM milk_tea AS m GROUP BY m.net_w HAVING SUM(m.sale_price) > 20;

SELECT m.net_w, SUM(m.sale_price) FROM milk_tea AS m WHERE m.net_w IN('100g', '150g')  GROUP BY m.net_w;
image.png
SELECT p.prod_name, p.sale_price, p.class FROM prod_info AS p WHERE p.sale_price > 10 GROUP BY p.prod_name HAVING p.class = '零食';
image.png
image.png
SELECT * FROM prod_info AS p;

SELECT p.class, COUNT(1) FROM prod_info AS p GROUP BY p.class;

SELECT p.class, AVG(p.sale_price) FROM prod_info AS p GROUP BY p.class HAVING COUNT(1) > 4;

SELECT p.class, COUNT(1) FROM prod_info AS p WHERE p.class != '零食' GROUP BY p.class;

SELECT p.class, COUNT(1) FROM prod_info AS p WHERE p.sale_price > 10 GROUP BY p.class;

SELECT p.prod_name, p.sale_price FROM prod_info AS p WHERE p.sale_price < 10 AND p.class = '零食' GROUP BY p.prod_name;

SELECT p.prod_name, p.sale_price FROM prod_info AS p WHERE p.sale_price < 10 AND p.class IN('零食') GROUP BY p.prod_name;

SELECT p.prod_name, p.sale_price, p.class FROM prod_info AS p WHERE p.sale_price > 10 GROUP BY p.prod_name HAVING p.class = '零食';
image.png

三.今日代码

-- COUNT SUM AVG MAX MIN

SELECT COUNT(1) FROM milk_tea AS m;

SELECT COUNT(m.prod_name) FROM milk_tea AS m;

SELECT COUNT(m.pro_date) FROM milk_tea AS m;

SELECT COUNT(m.sale_price) FROM milk_tea AS m;

SELECT COUNT(DISTINCT m.sale_price), COUNT(DISTINCT m.pro_date) FROM milk_tea AS m;

SELECT DISTINCT m.sale_price FROM milk_tea AS m;



SELECT SUM(m.in_price) FROM milk_tea AS m;  ---78.5

SELECT SUM(m.in_price * 1.1) FROM milk_tea AS m;  ---86.35

SELECT SUM(m.sale_price) FROM milk_tea AS m;  ---94.5

SELECT SUM(m.sale_price - m.in_price) FROM milk_tea AS m;  ---31.5

SELECT SUM(IFNULL(m.sale_price, 0) - m.in_price) FROM milk_tea AS m;  ---16



SELECT AVG(m.sale_price) FROM milk_tea AS m;  ---13.5

SELECT SUM(m.sale_price)/COUNT(m.sale_price) FROM milk_tea AS m;  ---13.5

SELECT SUM(m.sale_price)/COUNT(1) FROM milk_tea AS m;  ---11.8125


SELECT SUM(m.sale_price) FROM milk_tea AS m WHERE m.net_w = '100g';  ---34

SELECT SUM(m.sale_price) FROM milk_tea AS m WHERE m.net_w = '150g';  ---35



SELECT MAX(m.sale_price) FROM milk_tea AS m;

SELECT MIN(m.sale_price) FROM milk_tea AS m;



SELECT COUNT(m.sale_price), SUM(m.sale_price), AVG(m.sale_price), MIN(m.sale_price), MAX(m.sale_price) FROM milk_tea AS m;



SELECT m.net_w, SUM(m.sale_price) FROM milk_tea AS m GROUP BY m.net_w;

SELECT m.net_w, SUM(m.sale_price) FROM milk_tea AS m WHERE m.net_w IN ('100g', '150g') GROUP BY m.net_w;

SELECT m.net_w, COUNT(m.sale_price) FROM milk_tea AS m WHERE m.net_w IN('100g', '150g') GROUP BY m.net_w;




SELECT m.net_w, SUM(m.sale_price) FROM milk_tea AS m GROUP BY m.net_w HAVING SUM(m.sale_price) > 20;

SELECT m.net_w, SUM(m.sale_price) FROM milk_tea AS m WHERE m.net_w IN('100g', '150g')  GROUP BY m.net_w;


SELECT * FROM prod_info AS p;

SELECT p.class, COUNT(1) FROM prod_info AS p GROUP BY p.class;

SELECT p.class, AVG(p.sale_price) FROM prod_info AS p GROUP BY p.class HAVING COUNT(1) > 4;

SELECT p.class, COUNT(1) FROM prod_info AS p WHERE p.class != '零食' GROUP BY p.class;

SELECT p.class, COUNT(1) FROM prod_info AS p WHERE p.sale_price > 10 GROUP BY p.class;

SELECT p.prod_name, p.sale_price FROM prod_info AS p WHERE p.sale_price < 10 AND p.class = '零食' GROUP BY p.prod_name;

SELECT p.prod_name, p.sale_price FROM prod_info AS p WHERE p.sale_price < 10 AND p.class IN('零食') GROUP BY p.prod_name;

SELECT p.prod_name, p.sale_price, p.class FROM prod_info AS p WHERE p.sale_price > 10 GROUP BY p.prod_name HAVING p.class = '零食';

四.课后作业

Q1:使用聚合函数完成如下操作。
1、产品信息表中一共有多少产品?

SELECT COUNT(1) FROM prod_info;

2、有哪些产品类别,每个类别分别又有多少种产品?

SELECT p.class, COUNT(p.class) FROM prod_info AS p GROUP BY p.class;

3、一共有多少客户购买了商品?

SELECT COUNT(DISTINCT o.cust_id) FROM order_list AS o;

4、哪些客户购买了商品,分别购买了多少?

SELECT o.cust_id,COUNT(o.cust_id) FROM order_list AS o GROUP BY o.cust_id;

Q2:使用 WHERE 和 HAVING 对数据记录进行正确过滤。
1、各类别中,产品销售价在 10 块以上的分别有多少?

SELECT p.class, COUNT(1) AS quantity FROM prod_info AS p WHERE p.sale_price > 10 GROUP BY p.class;

2、各类别中,产品数量在 5 件以上的分别有哪些?

SELECT p.class,COUNT(1) AS numbers FROM prod_info AS p GROUP BY p.class HAVING COUNT(1) > 5;

3、购买商品超过 5 件的用户有哪些?

SELECT o.cust_id,SUM(o.quantity) AS numbers FROM order_list AS o GROUP BY o.cust_id HAVING SUM(quantity) > 5;

Q3:写出我们所学过的所有子句,以及在完整语句中的书写顺序。并且指出各子句的操作
对象以及使用注意事项。

答:

SELECT...FROM...WHERE...GROUP BY... HAVING...ORDER BY...;

SELECT...FROM... 是必须子句,SELECT的操作对象是列名,FROM的操作对象是表名。

WHERE...子句用来过滤行,操作对象是针对列的关系表达式。

GROUP BY...子句用来进行分组,操作对象是聚合键,也是列名。要求SELECT部分有聚合键。

HAVING...子句用来对分组结果进行筛选,操作对象是聚合函数,使用前提是必须存在GROUP BY子句。

ORDER BY...子句对查询结果进行排序,它一定是查询语句的最后一条句子。

你可能感兴趣的:(2021-07-26 SQL Day8)