简体   繁体   English

PostgreSQL中聚合和函数的区别

[英]Difference between aggregate and function in PostgreSQL

What is the difference between an aggregate and a function in PostgreSQL? PostgreSQL 中的聚合和函数有什么区别?

Is an aggregate just a specialized function?聚合只是一个专门的功能吗? (Aggregate constraint: Parameter must be a multiset, the return value must be a single value) (聚合约束:参数必须是多集,返回值必须是单值)

Can I use a function everywhere where an aggregate is possible?我可以在可以聚合的任何地方使用函数吗?

Are there any specialties when using either a PostgreSQL function or an aggregate?使用 PostgreSQL 函数或聚合时有什么特殊之处吗?

Aggregates are a subset of the functions.聚合是函数的子集。

Every aggregate is also a function (the input argument is of type array/set of values and the output is of type single scalar value )每个聚合也是一个函数(输入参数类型为array/set of values输出类型为single scalar value

You can replace each aggregate with any other aggregate - and get a different outcome - but if you replace it with a function which does not aggregate input values then you might get an error.您可以将每个聚合替换为任何其他聚合 - 并获得不同的结果 - 但如果将其替换为不聚合输入值的函数,则可能会出现错误。

The speciality comes from the definition - aggregates take a list, perform some calculations over its values and produce a single result (which is obviously not a list).特殊性来自定义 - 聚合获取一个列表,对其值执行一些计算并产生单个结果(这显然不是一个列表)。 Other functions might not work with list inputs - eg LOWER, UPPER, SQRT, and so on.其他功能可能不适用于列表输入 - 例如 LOWER、UPPER、SQRT 等。

The difference depends on perspective.差异取决于视角。 Internally aggregate function is executed as repeated calling of "cumulative" function (reads a value and updates state cumulative value) and "final" function (transforms cumulative value to final result).内部聚合函数作为“累积”函数(读取值并更新状态累积值)和“最终”函数(将累积值转换为最终结果)的重复调用执行。 From user's perspective, the aggregate functions are functions too, but with different purpose and usage.从用户的角度来看,聚合函数也是函数,但用途和用途不同。 The aggregate function and scalar functions are not replaceable.聚合函数和标量函数是不可替换的。 You cannot to use scalar function where aggregate function should be used, and you cannot to use aggregate function, where scalar function should be used.你不能在应该使用聚合函数的地方使用标量函数,你不能在应该使用标量函数的地方使用聚合函数。 Aggregate functions are calculated across rows, scalar functions are calculated just per one row.聚合函数跨行计算,标量函数仅计算每一行。 Syntax of aggregate and scalar function is same, but everything else is significantly different.聚合函数和标量函数的语法相同,但其他一切都显着不同。

Yes, aggregate functions are specialised.是的,聚合函数是专门的。 Consider almost any SQL documentation and in most (hopefully all) you will find a reference to functions with many types of functions listed.考虑几乎所有 SQL 文档,并且在大多数(希望是全部)中,您会找到对列出许多类型函数的函数的引用。 One of those types is "Aggregate functions".其中一种类型是“聚合函数”。 From an example list:从示例列表中:

Chapter 9. Functions and Operators
Table of Contents

9.1. Logical Operators
9.2. Comparison Functions and Operators
9.3. Mathematical Functions and Operators
9.4. String Functions and Operators
9.5. Binary String Functions and Operators
9.6. Bit String Functions and Operators
9.7. Pattern Matching
9.8. Data Type Formatting Functions
9.9. Date/Time Functions and Operators
...
9.21. Aggregate Functions

(Extract as at PostgreSQL 14). (从 PostgreSQL 14 中提取)。 See Functions and Aggregate Functions (current version links)请参阅函数聚合函数(当前版本链接)

You can substitute one aggregate function for another aggregate function eg you could swap MIN(column1) with MAX(column1) without causing a syntax error - but clearly the result could be very different.您可以将一个聚合函数替换为另一个聚合函数,例如,您可以将MIN(column1)MAX(column1)交换而不会导致语法错误 - 但显然结果可能非常不同。 However it isn't possible to just substitute any function for an aggregation.但是,不可能只用任何函数代替聚合。

" Can I use a function everywhere where an aggregate is possible? " 我可以在任何可能聚合的地方使用函数吗?

Not in the sense you can "swap" any function (as already discussed) but you can include non-aggregation functions in a group by query, eg:从某种意义上说,您不能“交换”任何函数(如已经讨论过的),但您可以group by中包含非聚合函数,例如:

select date_trunc('month', datecolumn), sum(quantity)
from table1
group by date_trunc('month', datecolumn)

" Are there any specialties when using either a PostgreSQL function or an aggregate? " 使用 PostgreSQL 函数或聚合时有什么特殊之处吗?

Yes and No. Fully SQL standard compliant functions (eg MIN/MAX/SUM/COUNT) are just that "standards compliant".是和否。完全符合 SQL 标准的函数(例如 MIN/MAX/SUM/COUNT)就是“符合标准”。 However more exotic functions do exist that may not exist in other dbms eg jsonb_object_agg() .然而,确实存在其他 dbms 中可能不存在的更多奇异函数,例如jsonb_object_agg() Use the documentation to explore the many available functions.使用文档来探索许多可用的功能。

One very useful specialised Postgres function (which returns a "set") is generate_series() eg一个非常有用的专用 Postgres 函数(它返回一个“集合”)是 generate_series() 例如

generate_series ( start timestamp, stop timestamp, step interval )

see: 9.25.见: 9.25。 Set Returning Functions 设置返回函数

Note: Functions can also be "user defined" (ie built by a user).注意:函数也可以是“用户定义的”(即由用户构建)。 So the complete list of functions available to you may include some of these as well - and these will be "highly specialised" as they may be unique to your organisation.因此,您可以使用的功能的完整列表也可能包括其中的一些 - 这些将是“高度专业化的”,因为它们可能是您的组织所独有的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM