【数据库】聚合和排序

news/2024/7/9 22:51:27 标签: mysql, postgresql

聚合函数:用于汇总的函数
COUNT:计数
SUM
AVG
MIN
MAX

3.1计算全部行数:

postgres=# SELECT COUNT(*)
postgres-# FROM Product;
 count
-------
     8
(1 行记录)

3.2COUNT某列不计入NULL值,COUNT(*)会计入

postgres=# SELECT COUNT(purchase_price)
postgres-# FROM Product;
 count
-------
     6
(1 行记录)

3.4SUM求某列的合计值

postgres=# SELECT SUM(sale_price)
postgres-# FROM Product;
  sum
-------
 16780
(1 行记录)

3.9MIN和MAX原则上适用于所有数据类型:

postgres=# SELECT MIN(regist_date)
postgres-# FROM Product;
    min
------------
 2008-04-28
(1 行记录)

3.10计算去除重复数据后的行数:

postgres=# SELECT COUNT(DISTINCT product_type)
postgres-# FROM Product
postgres-# ;
 count
-------
     3
(1 行记录)

3.12 SUM函数使用DISTINCT

postgres=# SELECT SUM(sale_price),SUM(DISTINCT sale_price)
postgres-# FROM Product;
  sum  |  sum
-------+-------
 16780 | 16280
(1 行记录)

GROUP BY分组语句
GROUP指定的句子叫聚合键
3.13按照商品种类分组计数

postgres=# SELECT product_type,COUNT(*)
postgres-# FROM Product
postgres-# GROUP BY product_type;
 product_type | count
--------------+-------
 衣服         |     2
 办公用品     |     2
 厨房用具     |     4
(3 行记录)

3.15 GROUP BY 和WHERE 连用

postgres=# SELECT purchase_price,COUNT(*)
postgres-# FROM Product
postgres-# WHERE product_type='衣服'
postgres-# GROUP BY purchase_price;
 purchase_price | count
----------------+-------
            500 |     1
           2800 |     1
(2 行记录)

GROUP的使用注意事项:
1.SELECT语句后面不可以写聚合键以外的列名
2.先执行GROUP,后执行SELECT,所以GROUP语句后面不可以写别名
3.GROUP的结果是无序的。
4.WHERE 语句不能使用聚合函数

HAVING子句:
3.20对分组后的结果选取数据行数为2的组。

postgres=# SELECT product_type,COUNT(*)
postgres-# FROM Product
postgres-# GROUP BY product_type
postgres-# HAVING COUNT(*)=2;
 product_type | count
--------------+-------
 衣服         |     2
 办公用品     |     2
(2 行记录)

3.22 用HAVING子句设定条件

postgres=# SELECT product_type,AVG(sale_price)
postgres-# FROM Product
postgres-# GROUP BY product_type
postgres-# HAVING AVG(sale_price)>500;
 product_type |          avg
--------------+-----------------------
 衣服         | 2500.0000000000000000
 厨房用具     | 2795.0000000000000000
(2 行记录)

ORDER BY语句:
ORDER BY后面跟的是排序键
3.28 按照销售单价从高到低排序

postgres=# SELECT product_id,product_name,sale_price,purchase_price
postgres-# FROM Product
postgres-# ORDER BY sale_price;
 product_id | product_name | sale_price | purchase_price
------------+--------------+------------+----------------
 0008       | 圆珠笔       |        100 |
 0006       | 叉子         |        500 |
 0002       | 打孔器       |        500 |            320
 0007       | 擦菜板       |        880 |            790
 0001       | T恤衫        |       1000 |            500
 0004       | 菜刀         |       3000 |           2800
 0003       | 运动T恤      |       4000 |           2800
 0005       | 高压锅       |       6800 |           5000
(8 行记录)

DESC:降序descendent
ASC:升序(可以不写)ascendent
3.29降序排列

postgres=# SELECT product_id,product_name,sale_price
postgres-# FROM Product
postgres-# ORDER BY sale_price DESC;
 product_id | product_name | sale_price
------------+--------------+------------
 0005       | 高压锅       |       6800
 0003       | 运动T恤      |       4000
 0004       | 菜刀         |       3000
 0001       | T恤衫        |       1000
 0007       | 擦菜板       |        880
 0002       | 打孔器       |        500
 0006       | 叉子         |        500
 0008       | 圆珠笔       |        100

3.30指定多个排序键:

postgres=# SELECT product_id,product_name,sale_price,purchase_price
postgres-# FROM Product
postgres-# ORDER BY sale_price,product_id;
 product_id | product_name | sale_price | purchase_price
------------+--------------+------------+----------------
 0008       | 圆珠笔       |        100 |
 0002       | 打孔器       |        500 |            320
 0006       | 叉子         |        500 |
 0007       | 擦菜板       |        880 |            790
 0001       | T恤衫        |       1000 |            500
 0004       | 菜刀         |       3000 |           2800
 0003       | 运动T恤      |       4000 |           2800
 0005       | 高压锅       |       6800 |           5000
(8 行记录)

注意:对含有NULL 的项目排序,会出现在开头或者末尾。
3.32ORDER 可以对别名进行排序

postgres=# SELECT product_id AS id,product_name,sale_price AS sp
postgres-# FROM Product
postgres-# ORDER BY sp,id;
  id  | product_name |  sp
------+--------------+------
 0008 | 圆珠笔       |  100
 0002 | 打孔器       |  500
 0006 | 叉子         |  500
 0007 | 擦菜板       |  880
 0001 | T恤衫        | 1000
 0004 | 菜刀         | 3000
 0003 | 运动T恤      | 4000
 0005 | 高压锅       | 6800
(8 行记录)

3.34ORDER BY可以使用SELECT中没有出现的列:

postgres=# SELECT product_id,product_name,sale_price,purchase_price
postgres-# FROM Product
postgres-# ORDER BY product_type
postgres-# ;
 product_id | product_name | sale_price | purchase_price
------------+--------------+------------+----------------
 0008       | 圆珠笔       |        100 |
 0002       | 打孔器       |        500 |            320
 0005       | 高压锅       |       6800 |           5000
 0004       | 菜刀         |       3000 |           2800
 0006       | 叉子         |        500 |
 0007       | 擦菜板       |        880 |            790
 0001       | T恤衫        |       1000 |            500
 0003       | 运动T恤      |       4000 |           2800
(8 行记录)

ORDER BY 可以使用SELECT 对应的列编号:

postgres=# SELECT product_id,product_name,sale_price,purchase_price
postgres-# FROM Product
postgres-# ORDER BY sale_price DESC,product_id;
 product_id | product_name | sale_price | purchase_price
------------+--------------+------------+----------------
 0005       | 高压锅       |       6800 |           5000
 0003       | 运动T恤      |       4000 |           2800
 0004       | 菜刀         |       3000 |           2800
 0001       | T恤衫        |       1000 |            500
 0007       | 擦菜板       |        880 |            790
 0002       | 打孔器       |        500 |            320
 0006       | 叉子         |        500 |
 0008       | 圆珠笔       |        100 |
(8 行记录)


postgres=# SELECT product_id,product_name,sale_price,purchase_price
postgres-# FROM Product
postgres-# ORDER BY 3 DESC,1;
 product_id | product_name | sale_price | purchase_price
------------+--------------+------------+----------------
 0005       | 高压锅       |       6800 |           5000
 0003       | 运动T恤      |       4000 |           2800
 0004       | 菜刀         |       3000 |           2800
 0001       | T恤衫        |       1000 |            500
 0007       | 擦菜板       |        880 |            790
 0002       | 打孔器       |        500 |            320
 0006       | 叉子         |        500 |
 0008       | 圆珠笔       |        100 |
(8 行记录)

练习:
3.2请编写一条SELECT 语句,求出销售单价(sale_price 列)合计值是 进货单价(purchase_price 列)合计值 1.5 倍的商品种类

postgres=# SELECT product_type,SUM(sale_price),SUM(purchase_price)
postgres-# FROM Product
postgres-# GROUP BY product_type
postgres-# HAVING SUM(sale_price)>1.5*SUM(purchase_price);
 product_type | sum  | sum
--------------+------+------
 衣服         | 5000 | 3300
 办公用品     |  600 |  320
(2 行记录)

3.3根据表格判断 ORDER BY语句

postgres=# SELECT *
postgres-# FROM Product
postgres-# ORDER BY regist_date DESC,sale_price;
 product_id | product_name | product_type | sale_price | purchase_price | regist_date
------------+--------------+--------------+------------+----------------+-------------
 0003       | 运动T恤      | 衣服         |       4000 |           2800 |
 0008       | 圆珠笔       | 办公用品     |        100 |                | 2009-11-11
 0006       | 叉子         | 厨房用具     |        500 |                | 2009-09-20
 0002       | 打孔器       | 办公用品     |        500 |            320 | 2009-09-20
 0001       | T恤衫        | 衣服         |       1000 |            500 | 2009-09-20
 0004       | 菜刀         | 厨房用具     |       3000 |           2800 | 2009-09-20
 0005       | 高压锅       | 厨房用具     |       6800 |           5000 | 2009-01-15
 0007       | 擦菜板       | 厨房用具     |        880 |            790 | 2008-04-28
(8 行记录)

http://www.niftyadmin.cn/n/1757260.html

相关文章

SQL基本用法总括一

整理一份SQL基本用法, 包含:①表: 创建,查询,修改,重命名 ②删除:字段,表,数据,主键,自增长 ③新增:字段,表 ④更新&…

【java笔记】final关键字、权限修饰符、内部类

/* final关键字,表示最终的,不可变的 可以修饰: 1.类 2.方法 3.成员变量 4.局部变量 */ final修饰一个类: package demo01; /* final修饰一个类,不能有任何子类(俗称太监类) final里的方法不可…

SQL基本用法总括二

SQL基本用法总括第二篇,能稍微高级一点点,但是也很常用, 包含:①过滤 ②通配符 ③计算字段 ④函数、子查询、组合查询 ⑤视图 ⑥存储过程 ⑦游标 ⑧触发器、事务管理 ⑨字符集 ⑩权限管理 详细如下: 一、过滤 wher…

【Java】常用的API:Math,System,Object,Arrays

1.Math 2.System package demo01;public class MySystem {public static void main(String[] args) {/*System.out.println("Start");System.exit(0);//终止当前运行的java虚拟机System.out.println("End");*/System.out.println(System.currentTimeMilli…

细聊 SELECT COUNT 用法

数据库查询相信很多人都不陌生,所有经常有人调侃程序员就是CRUD专员,这所谓的CRUD指的就是数据库的增删改查。 在数据库的增删改查操作中,使用最频繁的就是查询操作。而在所有查询操作中,统计数量操作更是经常被用到。 关于数据…

图解 Python 函数

函数是 “ 一系列命令的集合”,我们可以通过调用函数来自动执行某一系列命令。虽然经常性地出现于文章中的print()是被录入在Python的标准库中的函数,但是,程序员亦可创建自己的函数。 如果想要定义函数,则需要以“def 函数名():…

【数据库】INSERT,DELETE,UPDATE,事务

4.1 INSERT 4.1 INSERT插入一行数据: INSERT INTO ProductIns(product_id,product_name,product_type,sale_price,purchase_price,regist_date) VALUES(0001,T恤衫,衣服,1000,500,2009-09-20); INSERT 0 14.3 全列插入时,列清单可以省略 postgres# INS…

关于MySQL性能优化方式,这一篇就够!

小鱼已经很长时间没有更新博,主要是去年十月份转战到某上市大厂,由于在工业领域专业知识太薄弱,所以这段时间一直在嗷嗷补工业领域的专业知识, 最近在进入review开发代码时,发现一个问题,没有进行性能优化…