![]() |
|
Use the CREATE AGGREGATE statement to create a new aggregate function. User-defined aggregates extend the functionality of the database server because they can perform any kind of aggregate computation that the user wants to implement.
You can specify the INIT, ITER, COMBINE, FINAL, and HANDLESNULLS modifiers in any order.
Important: You must specify the ITER and COMBINE modifiers in a CREATE AGGREGATE statement. You do not have to specify the INIT, FINAL, and HANDLESNULLS modifiers in a CREATE AGGREGATE statement.
The ITER, COMBINE, FINAL, and INIT modifiers specify the support functions for a user-defined aggregate. These support functions do not have to exist at the time you create the user-defined aggregate.
If you omit the HANDLESNULLS modifier, rows with null aggregate argument values do not contribute to the aggregate computation. If you include the HANDLESNULLS modifier, you must declare all the support functions to handle null values as well.
The database server provides two ways to extend the functionality of aggregates. You use the CREATE AGGREGATE statement only for the second method.
In the following example, you create a user-defined aggregate named average:
Before you use the average aggregate in a query, you must also use CREATE FUNCTION statements to create the support functions specified in the CREATE AGGREGATE statement. The following table gives an example of the task that each support function might perform for average.
The database server can break up an aggregate computation into several pieces and compute them in parallel. The database server using the INIT and ITER support functions to compute each piece sequentially. Then the database server uses the COMBINE function to combine the partial results from all the pieces into a single result value. Whether an aggregate is parallel is an optimization decision that is transparent to the user.
Related statements: DROP AGGREGATE
For information about how to invoke a user-defined aggregate, see the discussion of user-defined aggregates in the Expression segment.
For a description of the sysaggregates system catalog table that holds information about user-defined aggregates, see the Informix Guide to SQL: Reference.
For a discussion of user-defined aggregates, see Extending Informix Dynamic Server 2000.