Implementing a user-defined aggregate function with C user-defined functions

About this task

To implement a user-defined aggregate function with C user-defined functions:

Procedure

  1. Determine the content and data type of the aggregate state.
  2. Write the C UDRs that implement the required aggregate support functions for the data type on which you want to implement the user-defined aggregate.
  3. Define the user-defined aggregate in the database with the CREATE AGGREGATE and CREATE FUNCTION statements.

Results

After you complete these steps, you can use the aggregate in an SQL statement.

The following sections describe each of these development steps in more detail and use the SQSUM1 user-defined aggregate (which A sample user-defined aggregate describes) as an example.
Table 1. A sample user-defined aggregate
User-Defined Aggregate Description Definition
SQSUM1 Sums all values and calculates the square of this sum (x1 + x2 + x3 + ... )2