avg()
Calculates the arithmetic mean of numeric values in a group.
Syntax
Parameters
EXPR- An expression that returns a numeric value to average.
Description
The avg() function accumulates the sum and count of numeric values returned by EXPR as it processes records in a group. Null values and non-numeric values are excluded from aggregation. It handles both integer types (integer, long) and floating-point types (float, double).
When aggregation is complete, the function returns the sum divided by the count as a 64-bit floating-point number (double). If there are no values to aggregate, it returns null.
This function can only be used in aggregation commands such as stats and timechart.
Error codes
| Code | Description |
|---|---|
| 91020 | The number of arguments is wrong. |
Usage examples
To prepare the WEB_APACHE_SAMPLE table used in these examples, refer to Preparing sample data.
-
Calculate the average response size for all requests
table WEB_APACHE_SAMPLE | stats avg(bytes) -
Calculate the average response size per HTTP method
table WEB_APACHE_SAMPLE | stats avg(bytes) by method -
Null value handling
json "[{'val': 10}, {'val': null}, {'val': 30}]" | stats avg(val) | # avg(val): 20.0
Compatibility
The avg() function has been available since before Logpresso Sonar 4.0.