CREATE AGGREGATE 中文man页面

系统
CREATE AGGREGATE 定义一个新的聚集函数。 一些用于基本类型的聚集函数如 min(integer) 和 avg(double precision) 等已经包含在基础软件包里了。 如果你需要定义一个新类型或需要一个还没有提供的聚集函数,这时便可用 CREATE AGGREGATE 来提供我们所需要的特性。

NAME

CREATE AGGREGATE - 定义一个新的聚集函数

SYNOPSIS

CREATE AGGREGATE name (
    BASETYPE = input_data_type,
    SFUNC = sfunc,
    STYPE = state_data_type
    [ , FINALFUNC = ffunc ]
    [ , INITCOND = initial_condition ]
)

DESCRIPTION 描述

CREATE AGGREGATE 定义一个新的聚集函数。 一些用于基本类型的聚集函数如 min(integer) 和 avg(double precision) 等已经包含在基础软件包里了。 如果你需要定义一个新类型或需要一个还没有提供的聚集函数,这时便可用 CREATE AGGREGATE 来提供我们所需要的特性。

如果给出了一个模式的名字(比如,CREATE AGGREGATE myschema.myagg ...),那么该聚集函数是在指定模式中创建的。 否则它是在当前模式中创建的。

一个聚集函数是用它的名字和输入数据类型来标识的。 同一模式中如果两个聚集处理的输入数据不同,它们可以有相同的名字。 一个聚集函数的输入数据类型必须和所有同一模式中的普通函数的名字和输入类型不同。

一个聚集函数是用一个或两个普通函数做成的: 一个状态转换函数 sfunc, 和一个可选的终计算函数 ffunc. 它们是这样使用的:

sfunc( internal-state, next-data-item ) ---> next-internal-state
ffunc( internal-state ) ---> aggregate-value

PostgreSQL 创建一个类型为 stype的临时变量。 它保存这个聚集的当前内部状态。 对于每个输入数据条目, 都调用状态转换函数计算内部状态值的新数值。 在处理完所有数据后,调用一次最终处理函数以计算聚集的返回值。 如果没有最终处理函数,那么将最后的状态值当做返回值。

一个聚集函数还可能提供一个初始条件,也就是说,所用的该内部状态值的初始值。 这个值是作为一个类型为 text 的字段存储在数据库里的, 不过它们必须是状态值数据类型的合法的外部表现形式的常量。 如果没有提供状态,那么状态值初始化为 NULL。

如果该状态转换函数被定义为 "strict", 那么就不能用 NULL 输入调用它。这个时候,带有这样的转换函数的聚集执行起来的现象如下所述。 NULL 输入的值被忽略(不调用此函数并且保留前一个状态值)。如果初始状态值是 NULL,那么由第一个非 NULL 值替换该状态值, 而状态转换函数从第二个非 NULL 的输入值开始调用。这样做让我们比较容易实现象 max 这样的聚集。 请注意这种行为只是当 state_type 与 input_data_type 相同的时候才表现出来。 如果这些类型不同,你必须提供一个非 NULL 的初始条件或者使用一个非strice的状态转换函数。

如果状态转换函数不是 strict(严格)的, 那么它将无条件地为每个输入值调用, 并且必须自行处理 NULL 输入和 NULL 转换值, 这样就允许聚集的作者对聚集中的空值有完全的控制。

如果终转换函数定义为"strict",则如果最终状态值是 NULL 时就不会调用它; 而是自动输出一个NULL的结果。(当然,这才是 strict 函数的正常特征。) 不管是那种情况,终处理函数可以选择返回 NULL。比如, avg 的终处理函数在零输入记录时就会返回 NULL。  

PARAMETERS 参数

name
要创建的聚集函数名(可以有模式修饰的)。
input_data_type
本聚集函数要处理的基本数据类型。 对于不检查输入类型的聚集来说,这个参数可以声明为"ANY"。 (比如 count(*))。
sfunc
用于处理源数据列里的每一个输入数据的状态转换函数名称。 它通常是一个双参数的函数,第一个参数的类型是 state_data_type 而第二个参数的类型是 input_data_type. 另外,对于一个不检查输入数据的聚集,该函数只接受一个类型为 state_data_type 的参数。 不管是哪种情况,此函数必须返回一个类型为 state_data_type的值。 这个函数接受当前状态值和当前输入数据条目,而返回下个状态值。
state_data_type
聚集的状态值的数据类型。
ffunc
在转换完所有输入域/字段后调用的最终处理函数。它计算聚集的结果。 此函数必须接受一个类型为 state_data_type 的参数。 聚集的输出数据类型被定义为此函数的返回类型。 如果没有声明 ffunc 则使用聚集结果的状态值作为聚集的结果,而输出类型为 state_data_type。
initial_condition
状态值的初始设置(值)。它必须是一个数据类型 state_data_type 可以接受的文本常量值。 如果没有声明,状态值初始为 NULL。

CREATE AGGREGATE 的参数可以以任何顺序书写,而不只是上面显示的顺序。

EXAMPLES 例子

参阅 ``User-defined Aggregates''  

COMPATIBILITY 兼容性

CREATE AGGREGATE 是 PostgreSQL 语言的扩展。 在 SQL 标准里没有 CREATE AGGREGATE。  

SEE ALSO 参见

ALTER AGGREGATE [alter_aggregate(7)], DROP AGGREGATE [drop_aggregate(l)]  

#p#

NAME

CREATE AGGREGATE - define a new aggregate function

SYNOPSIS

CREATE AGGREGATE name (
    BASETYPE = input_data_type,
    SFUNC = sfunc,
    STYPE = state_data_type
    [ , FINALFUNC = ffunc ]
    [ , INITCOND = initial_condition ]
)

DESCRIPTION

CREATE AGGREGATE defines a new aggregate function. Some aggregate functions for base types such as min(integer) and avg(double precision) are already provided in the standard distribution. If one defines new types or needs an aggregate function not already provided, then CREATE AGGREGATE can be used to provide the desired features.

If a schema name is given (for example, CREATE AGGREGATE myschema.myagg ...) then the aggregate function is created in the specified schema. Otherwise it is created in the current schema.

An aggregate function is identified by its name and input data type. Two aggregates in the same schema can have the same name if they operate on different input types. The name and input data type of an aggregate must also be distinct from the name and input data type(s) of every ordinary function in the same schema.

An aggregate function is made from one or two ordinary functions: a state transition function sfunc, and an optional final calculation function ffunc. These are used as follows:

sfunc( internal-state, next-data-item ) ---> next-internal-state
ffunc( internal-state ) ---> aggregate-value

PostgreSQL creates a temporary variable of data type stype to hold the current internal state of the aggregate. At each input data item, the state transition function is invoked to calculate a new internal state value. After all the data has been processed, the final function is invoked once to calculate the aggregate's return value. If there is no final function then the ending state value is returned as-is.

An aggregate function may provide an initial condition, that is, an initial value for the internal state value. This is specified and stored in the database as a column of type text, but it must be a valid external representation of a constant of the state value data type. If it is not supplied then the state value starts out null.

If the state transition function is declared ``strict'', then it cannot be called with null inputs. With such a transition function, aggregate execution behaves as follows. Null input values are ignored (the function is not called and the previous state value is retained). If the initial state value is null, then the first nonnull input value replaces the state value, and the transition function is invoked beginning with the second nonnull input value. This is handy for implementing aggregates like max. Note that this behavior is only available when state_data_type is the same as input_data_type. When these types are different, you must supply a nonnull initial condition or use a nonstrict transition function.

If the state transition function is not strict, then it will be called unconditionally at each input value, and must deal with null inputs and null transition values for itself. This allows the aggregate author to have full control over the aggregate's handling of null values.

If the final function is declared ``strict'', then it will not be called when the ending state value is null; instead a null result will be returned automatically. (Of course this is just the normal behavior of strict functions.) In any case the final function has the option of returning a null value. For example, the final function for avg returns null when it sees there were zero input rows.  

PARAMETERS

name
The name (optionally schema-qualified) of the aggregate function to create.
input_data_type
The input data type on which this aggregate function operates. This can be specified as "ANY" for an aggregate that does not examine its input values (an example is count(*)).
sfunc
The name of the state transition function to be called for each input data value. This is normally a function of two arguments, the first being of type state_data_type and the second of type input_data_type. Alternatively, for an aggregate that does not examine its input values, the function takes just one argument of type state_data_type. In either case the function must return a value of type state_data_type. This function takes the current state value and the current input data item, and returns the next state value.
state_data_type
The data type for the aggregate's state value.
ffunc
The name of the final function called to compute the aggregate's result after all input data has been traversed. The function must take a single argument of type state_data_type. The return data type of the aggregate is defined as the return type of this function. If ffunc is not specified, then the ending state value is used as the aggregate's result, and the return type is state_data_type.
initial_condition
The initial setting for the state value. This must be a string constant in the form accepted for the data type state_data_type. If not specified, the state value starts out null.

The parameters of CREATE AGGREGATE can be written in any order, not just the order illustrated above.

EXAMPLES

See the section called ``User-defined Aggregates'' in the documentation.  

COMPATIBILITY

CREATE AGGREGATE is a PostgreSQL language extension. The SQL standard does not provide for user-defined aggregate function.  

SEE ALSO

ALTER AGGREGATE [alter_aggregate(7)], DROP AGGREGATE [drop_aggregate(l)]

责任编辑:韩亚珊 来源: CMPP.net
相关推荐

2011-08-24 09:02:10

ALTER AGGRE中文man

2011-08-24 14:06:36

DROP AGGREG中文man

2011-08-24 10:56:32

CREATE CONV中文man

2011-08-24 13:46:39

CREATE VIEW中文man

2011-08-24 13:43:09

CREATE USER中文man

2011-08-24 13:29:20

CREATE TABL中文man

2011-08-24 11:15:24

CREATE INDE中文man

2011-08-24 13:36:25

CREATE TRIG中文man

2011-08-24 13:32:56

CREATE TABL中文man

2011-08-24 13:26:19

CREATE SEQU中文man

2011-08-24 11:02:11

CREATE DOMA中文man

2011-08-24 11:05:36

CREATE FUNC中文man

2011-08-24 13:39:44

CREATE TYPE中文man

2011-08-24 13:23:10

CREATE SCHE中文man

2011-08-24 10:59:19

CREATE DATA中文man

2011-08-24 11:18:53

CREATE LANG中文man

2011-08-24 11:10:17

CREATE GROU中文man

2011-08-24 11:23:20

CREATE OPER中文man

2011-08-24 11:31:47

CREATE RULE中文man

2011-08-25 14:07:55

create_modu中文man
点赞
收藏

51CTO技术栈公众号