Determine the aggregate state
An aggregate is a series of iterations. Each iteration processes one aggregate argument (which contains one column value) and performs the necessary computations to merge it into a partial result. The partial result is a snapshot of the aggregate arguments that the aggregate has merged so far. After the aggregate has received all column values, it returns a value to the calling statement, based on the final partial result.
- Its own local variables, which are deallocated at the end of each iteration and therefore unavailable to other iterations
- The aggregate argument, which contains a new column value for each iteration
- The aggregate state, which contains any nonlocal information that the iteration needs to perform its merge, including the partial result and any other external information (such as an operating-system file) that an iteration might need to access
As a starting point, try using the data type of the aggregate argument for the aggregate state. Such a state is called a simple state. For more information, see Aggregate support functions for the aggregate state.
- The aggregate argument, which is passed to each iteration of the aggregate
- The partial sum of previous argument values, which must exist in the aggregate state
The data type that you choose for the aggregate state affects how the state must be managed. When the SQSUM1 aggregate receives INTEGER values as its aggregate arguments, the sum of these values is also an INTEGER value. Therefore, the SQSUM1 aggregate has an integer state, which holds the partial sum.