SumIf

SumIf — Static aggregate function for conditional sum

SYNOPSIS

SumIf (pred1 ? expr1 ; pred2 ? expr2 ; ... ; default-expr)

DESCRIPTION

The function name is case-sensitive and contains upper-case ‘S’ and ‘I’. This function exists for static (table-based) aggregation only.

Computes a value for each record from the base table and returns the sum.

Predicates pred1, pred2, and so forth must be of type bool. Expressions expr1, expr2, and so forth must each be a StreamBase expression of type double, int, long, or timestamp. If the expressions are of numeric types, the expressions and the returned value can be of different types; StreamBase CEP type coercion rules apply, and each expression must be of a type that can be promoted to the type of the returned value; see Data Type Coercion. Otherwise, all expressions must be of the same type, and the returned value is of that type. The values to compute are determined by evaluation of one or more predicates. For each record from the base table, the predicates are evaluated in left-to-right order. If a predicate evaluates to true, then the computed value for that record is the value of the corresponding expression, and no further predicates are evaluated. If no predicate evaluates to true, then the computed value for that record is the value of the default expression.

SumIf returns the sum of these computed values for all records from the base table.

In all predicates and expressions, any valid StreamBase expression can be used, and field references must match table field names.

If the expressions are timestamps, see Simple Functions: Timestamp Overview for how to add timestamps.

The general form of SumIf, as shown above, is equivalent to the following statement:

if (pred1)
    value = expr1;
else if (pred2)
    value = expr2;
...
else
    value = default-expr;

where value is the computed value for a given record from the base table. SumIf returns the sum of these values.

The simplest form of SumIf has a single predicate:

SumIf (pred ? expr ; default-expr)

This form is analogous to the question-mark-colon syntax of C and Java, but uses a semicolon instead of a colon, as shown below:

value = pred ? expr : default-expr