SYNOPSIS
CREATE AGGREGATE name ( BASETYPE = input_data_type, SFUNC = sfunc, STYPE = state_data_type [ , FINALFUNC = ffunc ] [ , INITCOND = initial_condition ] )
DESCRIPTION Description
CREATE AGGREGATE defines a new aggregate functions. Some aggregate functions for base types such as min (integer) and avg (double precision) and the like have been included in the basis software package. If you need to define a new type or a need not provide aggregate functions, then CREATE AGGREGATE can be used to provide the features we need.
If given the name of a model (for example, CREATE AGGREGATE myschema.myagg ...), then the aggregate function is created in the specified mode. Otherwise it is created in the current mode.
Aggregate is a function name and its data type identifier input. If the input data in the same mode in two different aggregation treatment, they may have the same name. An aggregate function of the input data type and must enter all the names and the same mode of ordinary function of different types.
Aggregate function is a one or two ordinary functions: a state transition function sfunc, and an optional final calculation function ffunc. These are used:
sfunc( internal-state, next-data-item ) ---> next-internal-state ffunc( internal-state ) ---> aggregate-value
PostgreSQL creates a temporary variable stype of. It save the current internal state of aggregation. For each entry in the input data, it is counted value of the internal state value of the new call state transition function. After all the data has been processed, a final function call returns the value to calculate the aggregate. If there is no final function then the ending state value is returned.
An aggregate function may also provide an initial condition, i.e., the initial value of the internal state value is used. This value is used as a type of text field stored in the database, but they must be a valid external representation of a constant state value data type. If no state, then the state value is initialized to NULL.
If the state transition function is declared "strict", then it can not be used NULL enter the call. This time, aggregation phenomenon with such a transfer function as follows to implement. NULL value input is ignored (do not call this function and the previous state value is retained). If the initial state value is NULL, then this state value is replaced by the first non-NULL value, and the state transition function is invoked beginning with the second input non-NULL value. Let us do so more easily implementing aggregates like max. Note that this behavior only when state_type and input_data_type same when it is manifested. If the types are different, you must provide a non-NULL initial condition or use a non strice transition function.
If the state transition function is not strict (strict), then it will unconditionally call for each input value, and must deal with NULL inputs and NULL transition values for itself, which allows the aggregate author to have full control over the null values in the aggregate .
If the final function is defined as "strict", when the value is NULL if the final state will not be called; instead automatically output a NULL result. (Of course, this is the normal behavior of strict functions.) In any case the final function has the option of returning NULL. For example, avg final function at zero input record returns NULL.
PARAMETERS Parameters
- name
- Aggregate function name to be created (optionally schema modifications).
- input_data_type
- Basic data types present in the aggregate function. Do not check for gathering input types, this parameter can be specified as "ANY". (For example, count (*)).
- sfunc
- Status column in the source data for processing each input data into a function name. It is usually a two-parameter function of the first parameter and the second parameter state_data_type is input_data_type The input. Further, for an aggregate check data input, the function accepts one argument of type of state_data_type. In either case, this function must return a value of type of 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
- Data type aggregated state value.
- ffunc
- After the final function converts all input / field calls. It results aggregation. This function must accept one argument of type of state_data_type. Aggregated output data type is defined for this type of the function. If not specified ffunc aggregated results using state value as a result of aggregation, and the output type is state_data_type.
- initial_condition
- The initial value setting state (value). It must be a data type of text state_data_type acceptable constant value. If not specified, the state value starts to NULL.
CREATE AGGREGATE parameters can be written in any order, not just in the order shown above.