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...子句对查询结果进行排序,它一定是查询语句的最后一条句子。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,294评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,493评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,790评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,595评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,718评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,906评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,053评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,797评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,250评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,570评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,711评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,388评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,018评论 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,796评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,023评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,461评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,595评论 2 350

推荐阅读更多精彩内容

  • select distinct [字段名] from [表名];用于查询该列不重复的所有值。 where子句中可以...
    Ew0828阅读 282评论 0 0
  • 2017/3/14 RDBMS:关系型数据库管理系统 关系模型独立于语言 SQL有几种不同类型的语言:数据定义语言...
    ancherl阅读 1,605评论 0 6
  • 简介 GreenDao是一个简化数据库开发的ORM(Object Ralativational Mapping)来...
    3Q竹林阅读 6,000评论 0 3
  • SQL 必知必会 第1章 了解SQL 定义 结构化查询语言(Structured Query Language) ...
    zzzzzzzeng阅读 1,283评论 0 0
  • Spark SQL Spark SQL的概述 Hive的诞生,主要是因为开发MapReduce程序对 Java 要...
    奋斗的蛐蛐阅读 1,793评论 0 7