聚合函数:用于汇总的函数
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 行记录)