sum

The sum aggregate calculates the cumulative sum of a group of values. 

Synopsis

AFL% aggregate(array,sum(attribute)[,dimension_1,dimension_2,...])
AQL% SELECT sum(attribute) FROM array [GROUP BY dimension_1,dimension_2,...]

Summary

The sum aggregate calculates the cumulative sum of a group of values. You can optionally specify one or more dimensions to group by. The sum of an empty set is 0. The sum of a set that contains only NULL values is also 0. If the set contains NULL and NOT NULL values, the result is the sum of all the NOT NULL values.

Example

To sum the columns and rows of a 3×3 array, do the following:

  1. Create a 1-attribute, 2-dimensional array called m3x3:

    AFL% CREATE ARRAY m3x3 <val:double>[i=0:2; j=0:2];
  2. Store values of 0–8 in m3x3:

    AFL% store(build(m3x3,i*3+j),m3x3); 


    The output is:

    {i,j} val
    {0,0} 0
    {0,1} 1
    {0,2} 2
    {1,0} 3
    {1,1} 4
    {1,2} 5
    {2,0} 6
    {2,1} 7
    {2,2} 8
  3. Sum the values of m3x3 along dimension j. This sums the columns of m3x3:

    AFL% aggregate(m3x3,sum(val),j);  


    The output is:

    {j} val_sum
    {0} 9
    {1} 12
    {2} 15
  4. Sum the values of m3x3 along dimension i. This sums the rows of m3x3:

    AFL% aggregate(m3x3,sum(val),i); 


    The output is:

    {i} val_sum
    {0} 3
    {1} 12
    {2} 21
  5. Remove the array:

    AFL% remove(m3x3);