var()

Calculates the population variance of numeric values returned by an expression.

Syntax

var(NUM_EXPR)

Parameters

NUM_EXPR
An expression that returns the numeric values for which to calculate the variance.

Description

The var() function calculates variance using Welford's online algorithm. The return value is the population variance, which is the sum of squared deviations divided by the number of records (N). Null values and non-numeric values are ignored. If there are no values, it returns null. If there is exactly one value, it returns 0.0. The return value is a 64-bit floating-point number (double).

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 variance of response sizes across all requests

    table WEB_APACHE_SAMPLE | stats var(bytes)
    
  2. Calculate the variance of response sizes per HTTP method

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

    json "[{'val': 10}, {'val': null}, {'val': 30}]"
    | stats var(val)
    | # Null values are ignored.
    

Compatibility

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