2.7 聚合函数

像其他数据库产品一样,PostgreSQL支持聚合函数。聚合函数将多行输入处理后,输出为一行。例如:有计算数据集的总数(count)、总和(sum)、平均值(avg)、最大值(max)、最小值(min)的聚合函数。

例如,可以通过以下语句获取各个城市temp_lo中的最大值:

SELECT max(temp_lo) FROM weather;

max

-----

46

(1 row)

如果想要知道哪个城市的temp_lo值为最大值,那么你可能会执行下面的错误语句:

SELECT city FROM weather WHERE temp_lo = max(temp_lo); --WRONG

因为聚合函数max不能放到where中,所以上面的语句会报错。(存在这个限制的原因是,where子句限定了需要聚合函数计算的数据集,所以where子句中的数据要在使用聚合函数之前选出来,所以where子句中不能使用聚合函数)。然而,可以使用子查询满足该要求:

SELECT city FROM weather

WHERE temp_lo = (SELECT max(temp_lo) FROM weather);

city

---------------

San Francisco

(1 row)

子查询与外部查询是相互独立的,所以子查询中可以使用聚合函数。

聚合函数与group by配合使用,效果更好。比如,我们可以使用以下语句获得监测到的每个城市的最低温度的最高值:

SELECT city, max(temp_lo)

FROM weather

GROUP BY city;

city      | max

---------------+-----

Hayward       |  37

San Francisco |  46

(2 rows)

上面的查询,每个城市给出一个结果。每个聚合结果是通过对符合条件的行分城市进行计算得到的。我们还可以使用HAVING对分组聚合结果进行筛选:

SELECT city, max(temp_lo)

FROM weather

GROUP BY city

HAVING max(temp_lo) < 40;

city   | max

---------+-----

Hayward |  37

(1 row)

这样只会返回temp_lo均低于40的行。接下来,假设我们只想选出以“S”开头的城市数据,那么可以:

SELECT city, max(temp_lo)

FROM weather

WHERE city LIKE 'S%' -- LIKE匹配行,在第9.7.1章节进行解释。

GROUP BY city

HAVING max(temp_lo) < 40;

了解聚合与SQL的WHERE和HAVING子句之间的交互非常重要。WHERE与HAVING之间的本质区别是:where在聚合之前选取行(也就是说,where控制哪些行进入聚合计算。),而having是筛选聚合之后的行。因此,where字句中不能包含聚合函数;因为使用聚合函数去决定哪些行由聚合函数计算,毫无意义。另一方面,HAVING字句中总是包含聚合函数。(严格来说,HAVING后可以不加聚合函数,但这基本毫无用处。而可以使用where字句更好的满足相应的需求。)

在前面的示例中,我们可以将对城市名称的过滤放到where字句中,这比将对城市名称的过滤放到HAVING字句中更有效率。因为放到where字句中进行过滤,将过滤之后的数据进行聚合计算,从而可以减少聚合的计算量。

2.7. Aggregate Functions

Like most other relational database products, PostgreSQL supports aggregate functions. An aggregate function computes a single result from multiple input rows. For example, there are aggregates to compute the count, sum, avg (average), max (maximum) and min (minimum) over a set of rows.

As an example, we can find the highest low-temperature reading anywhere with:

SELECT max(temp_lo) FROM weather;

max

-----

46

(1 row)

If we wanted to know what city (or cities) that reading occurred in, we might try:

SELECT city FROM weather WHERE temp_lo = max(temp_lo); WRONG

but this will not work since the aggregate max cannot be used in the WHERE clause. (This restriction exists because the WHERE clause determines which rows will be included in the aggregate calculation; so obviously it has to be evaluated before aggregate functions are computed.) However, as is often the case the query can be restated to accomplish the desired result, here by using a subquery:

SELECT city FROM weather

WHERE temp_lo = (SELECT max(temp_lo) FROM weather);

city

---------------

San Francisco

(1 row)

This is OK because the subquery is an independent computation that computes its own aggregate separately from what is happening in the outer query.

Aggregates are also very useful in combination with GROUP BY clauses. For example, we can get the maximum low temperature observed in each city with:

SELECT city, max(temp_lo)

FROM weather

GROUP BY city;

city | max

---------------+-----

Hayward | 37

San Francisco | 46

(2 rows)

which gives us one output row per city. Each aggregate result is computed over the table rows matching that city. We can filter these grouped rows using HAVING:

SELECT city, max(temp_lo)

FROM weather

GROUP BY city

HAVING max(temp_lo) < 40;

city | max

---------+-----

Hayward | 37

(1 row)

which gives us the same results for only the cities that have all temp_lo values below 40. Finally, if we only care about cities whose names begin with “S”, we might do:

SELECT city, max(temp_lo)

FROM weather

WHERE city LIKE 'S%' -- The LIKE operator does pattern matching and is explained in Section 9.7.

GROUP BY city

HAVING max(temp_lo) < 40;

It is important to understand the interaction between aggregates and SQL's WHERE and HAVING clauses. The fundamental difference between WHERE and HAVING is this: WHERE selects input rows before groups and aggregates are computed (thus, it controls which rows go into the aggregate computation),whereas HAVING selects group rows after groups and aggregates are computed. Thus, the WHERE clause must not contain aggregate functions; it makes no sense to try to use an aggregate to determine which rows will be inputs to the aggregates. On the other hand, the HAVING clause always contains aggregate functions. (Strictly speaking, you are allowed to write a HAVING clause that doesn't use aggregates, but it's seldom useful. The same condition could be used more efficiently at the WHERE stage.)

In the previous example, we can apply the city name restriction in WHERE, since it needs no aggregate.This is more efficient than adding the restriction to HAVING, because we avoid doing the grouping and aggregate calculations for all rows that fail the WHERE check.

2.7. Aggregate Functions相关推荐

  1. Oracle 聚合函数(Aggregate Functions)说明

    Oracle Aggregate Functions用过很多,官网的说明如下: Aggregate Functions http://docs.oracle.com/cd/E11882_01/serv ...

  2. Aggregate functions cannot be used in the select right after the flatAggregate

    代码如下: Table groupByDistinctResult = orders.window(Tumble.over(lit(5).minutes())                 .on( ...

  3. SQL--合计函数(Aggregate functions):avg,count,first,last,max,min,sum

    SQL--合计函数(Aggregate functions):avg,count,first,last,max,min,sum avg() 函数 定义和用法 AVG 函数返回数值列的平均值.NULL ...

  4. flink表聚合函数(Table aggregate Functions)

    用户定义的表聚合函数(User-Defined Table Aggregate Functions) ,可以把一个表中数据,聚合卫具有多行和多列的结果表用户定义表聚合函数,是通过继承TableAggr ...

  5. Spark SQL 内置函数(五)Aggregate Functions(基于 Spark 3.2.0)

    前言 本文隶属于专栏<1000个问题搞定大数据技术体系>,该专栏为笔者原创,引用请注明来源,不足和错误之处请在评论区帮忙指出,谢谢! 本专栏目录结构和参考文献请见1000个问题搞定大数据技 ...

  6. 【大数据开发】flink1.12 Aggregate Functions 遇到的坑

    以下记录的问题,Flink版本为1.12.7 问题原因自定义  import org.apache.flink.table.functions.AggregateFunction 测试 报错: Can ...

  7. clickhouse Parametric Aggregate Functions的使用

    sequenceMatch sequenceMatch(pattern)(timestamp, cond1, cond2, -) 检查序列是否包含与模式匹配的事件链 参数 pattern 模式字符串 ...

  8. Analytic Functions in Oracle 8i and 9i

    原文地址:http://balance9.bokee.com/5534690.html Analytic Functions in Oracle 8i and 9i Oracle 8i and 9i分 ...

  9. 4.2.7. Aggregate Expressions

    4.2.7. Aggregate Expressions 4.2.7.聚合表达式 An aggregate expression represents the application of an ag ...

最新文章

  1. 一文带你玩转设计模式之「责任链」
  2. Cannot create a session after the response has been committed
  3. 记录下Linux难记实用的命令
  4. python3 centos7 Python.h无法找到
  5. 项目开发流程_绿维文旅:旅游项目开发模式与流程
  6. c web mysql数据库_C连接Mysql数据库代码
  7. 参考平面及其高度_柱面及其方程
  8. 三色标记原理,我给应聘者问懵了...
  9. 如何快速python入手_初学者怎么才能快速学会Python?
  10. 在吗?我要讲件大事了,你绝对不知道CSDN公众号还有这个功能!错过后悔!
  11. oracle flashback 功能,oracle 10g中开启flashback功能
  12. Windows下nmap命令及Zenmap工具的使用方法
  13. EMBA必看书籍推荐
  14. Xcode8/iOS10 升级后遇到的问题小结
  15. 混合高斯背景建模原理
  16. 问卷星 php,问卷星API介绍
  17. Java小游戏-幸运抽奖-进阶版(可多抽取多次)
  18. 最近发现的一个学习宝库
  19. U盘格式化了怎么恢复数据,三步操作轻松完成
  20. 图片清晰度评价-java实现

热门文章

  1. 详解CentOS 7中RAID 6与RAID 10配置(理论+实践)
  2. c语言解析sql语句_如何在C语言里面执行SQL语句?
  3. windows10共享打印机设置
  4. k线图中趋势线的画法精讲
  5. Matlab处理气象数据(七)分段趋势线的做法
  6. android系统精简掉music.apk后设置铃声的方法
  7. 【Linux operation 23】Win 10 64位(X86 架构CPU)安装ARM架构的虚拟机(银河麒麟高级服务器操作系统 V10)
  8. Jquery删除节点元素
  9. 曼哈顿距离和切比雪夫距离的转换
  10. 芴甲氧羰酰基-氨基-聚乙二醇-巯基吡啶Fmoc-NH-PEG-OPSS