#### Sum

Function

- Sum(array_variable);
- Sum(field_variable);
- Sum(field_variable, groupby);

Description

This function is used to add a group of values (referred to by the first argument) together. The second argument, if there is one, gives the group by field name.

Arguments

- field_variable
  field_variable is a special variable in Formula, and refers to a group of values from a database or formula field. Whatever kind of source the data comes from, database or formula, it should be of the Number or Currency data type.
- array_variable
  array_variable refers to an array whose element should be Number or Currency value.
- groupby
  groupby may be a constant String to indicate the groupby field name, or a field variable on which grouping occurs, or a parameter variable that can accept a different value before each execution of a report.

Return type

The result type relies on the first argument. It may be Number or Currency if the data type of each element in the first argument is Number or Currency respectively.

Examples

- `Number x = Sum([1.0, 2.0, 3.0, 4.5, 45.0, 67.0])`
- `Number x[6] = [1.0, 2.0, 3.0, 4.5, 45.0, 67.0];
  Number y = Sum(x)`
- `Currency x = Sum([$1, $2, $3.0, $4.5, $45, $67])`
- `Currency x[] = [$1, $2, $3.0, $4.5, $45, $67];
  Currency y = Sum(x)`
- `Number x = Sum(@dbfield)`
- `Number x = Sum(@formula)`
- `Number x = Sum(@dbfield, "group_field")`
- `Number x = Sum(@dbfield, @"group_field")`
- `Number x = Sum(@dbfield, @parameter)`
- `Currency x = Sum(@dbfield)`
- `Currency x = Sum(@formula)`
- `Currency x = Sum(@formula, "group_field")`
- `Currency x = Sum(@formula, @"group_field")`
- `Currency x = Sum(@formula, @parameter)`

Note: If a field_variable is assigned to a variable, for example X, the variable X will lose the characteristic of representing a group of values. The following formula will then be treated as incorrect.

`Number x = @dbfield;
Number y = Sum(x); // system will prompt you that there is no such kind of function.`