sum()

Calculates the sum of numeric values returned by an expression.

Syntax

sum(NUM_EXPR)

Parameters

NUM_EXPR
An expression that returns the numeric values to sum.

Description

The sum() function evaluates NUM_EXPR for each record and accumulates the numeric sum. It tracks integers (long) and floating-point numbers (double) separately. If only integer values are present, it returns a 64-bit integer. If any floating-point values are present, it adds the integer sum and the floating-point sum and returns the result. Null values and non-numeric values are excluded from aggregation. If all input values are null or non-numeric, it returns null.

Use this function in aggregation commands such as stats and timechart.

Error codes

CodeDescription
91020The number of arguments is incorrect.

Usage examples

To prepare the WEB_APACHE_SAMPLE table used in these examples, refer to Preparing sample data.

  1. Calculate the total response size across all requests

    table WEB_APACHE_SAMPLE | stats sum(bytes)
    
  2. Calculate the total response size per HTTP method

    table WEB_APACHE_SAMPLE | stats sum(bytes) by method
    
  3. Null value handling

    json "[{'val': 10}, {'val': null}, {'val': 30}]"
    | stats sum(val)
    | # sum(val): 40
    

Compatibility

The sum() function has been available since before Logpresso Sonar 4.0.