On this page
stats command
Aggregate matching events - count, sum, avg, min, max, latest value, percentiles, distinct counts, distinct values.
Calculates aggregate statistics, such as a count, sum or percentile, over the events that match the search. Without a by clause it returns a single row. With one, it returns one row for each distinct combination of the by fields.
After stats, the result is the aggregate table rather than events. Later stages work with its columns: the by fields and one column per aggregation.
Syntax
| stats <function>(<field>) [AS <name>] [, <function>(<field>) [AS <name>] ...] [by <field> [, <field> ...]]
| stats <function>(<field>) <operator> <value>Square brackets mark an optional part, and angle brackets a value you supply.
Arguments
| Argument | Required | Description |
|---|---|---|
<function>(<field>) | Required | An aggregation from the list below. count() takes no field, and every other function needs one. Separate several aggregations with commas. |
AS <name> | Optional | Names the result column. Without it the column is named after the call, such as count or avg_duration_ms. A quoted name may contain spaces: AS "Total Errors". |
by <field>, ... | Optional | Groups the results by one or more fields, separated by commas or spaces. |
<operator> <value> | Optional | A shorthand threshold on the first aggregation. | stats count() > 50 is the same as | stats count() | where count > 50, and is the usual shape for an alert condition. |
Aggregation functions
| Function | Returns | Summary |
|---|---|---|
count | number | Number of matching events in each group. |
sum | number | Total of a numeric field. |
avg | number | Mean of a numeric field. |
min | number | Smallest value of a field. |
max | number | Largest value of a field. |
latest | number | Most recent numeric value of a field. |
p50 | number | Median (50th percentile) of a numeric field. |
p95 | number | 95th percentile of a numeric field. |
p99 | number | 99th percentile of a numeric field. |
distinct_count | number | Number of distinct values of a field. |
values | text | The distinct values of a field, sorted. |
count()
Counts the events, or rows, in each group. It takes no field: to count the different values a field holds, use distinct_count(field).
Returns: number
| stats count() by servicesum(<field>)
Adds up a numeric field across each group. Values that are not numbers are skipped rather than counted as zero.
Returns: number
| stats sum(bytes) AS total_bytes by hostavg(<field>)
The arithmetic mean of a numeric field across each group, ignoring events that do not carry a number for it.
Returns: number
| stats avg(duration_ms) by routemin(<field>)
The smallest value of a numeric field in each group. On the timestamp it gives the earliest event's time.
Returns: number
| stats min(duration_ms) by servicemax(<field>)
The largest value of a numeric field in each group. On the timestamp it gives the newest event's time.
Returns: number
| stats max(memory_used_mb) by hostlatest(<field>)
The field's numeric value on the newest event that carries one. Use it for a gauge, such as queue depth or free disk, where the current reading matters more than the peak.
Returns: number
| stats latest(queue_depth) by queuep50(<field>)
The median: half the values in each group are at or below it. Less affected by a few extreme values than avg(). Percentiles are calculated as close estimates.
Returns: number
| stats p50(duration_ms) by routep95(<field>)
The value 95% of the group is at or below. The usual measure of what a slow but not unusual request looks like.
Returns: number
| stats p95(duration_ms) by routep99(<field>)
The value 99% of the group is at or below. Shows the tail latency a small share of requests still sees.
Returns: number
| stats p99(duration_ms) by servicedistinct_count(<field>)
How many different values a field takes in each group, such as the number of users or hosts affected. The count is a close estimate on very large groups.
Returns: number
| stats distinct_count(user_id) AS users by countryvalues(<field>)
The group's distinct values of a field as one comma-separated piece of text, deduplicated and sorted, up to 100 values per group. Events without the field are skipped. The only aggregation that returns text rather than a number.
Returns: text
| stats values(host) by serviceUsage
- Every aggregation returns a number except
values(), which returns text. - Numeric functions skip values that are not numbers rather than treating them as zero.
- After stats, refer to an aggregate by its AS name, its column name (
count) or the way it was written (count()). For example:| stats count() by repository | fields repository, count(). - extract and json must come before stats. Once events are aggregated, their raw text is no longer available.
- To aggregate into time buckets for a chart, use timechart instead.
Examples
1. Count errors per service
level="error" | stats count() by serviceReturns one row per service with the number of error events it logged.
2. Several statistics in one pass
service=checkout-api | stats count(), avg(duration_ms), p95(duration_ms) by routeRequest count, mean latency and 95th percentile latency for every route, side by side.
3. Give a column a readable name
level="error" | stats count() AS "Total Errors" by service | sort -"Total Errors"Names the count column and sorts by it, busiest first.
4. How many users each country had
event=login | stats distinct_count(user_id) AS users by countryCounts distinct users rather than login events.
5. Alert when errors pass a threshold
service=checkout-api level="error" | stats count() > 50Returns a row only when there are more than 50 matching errors in the time range.
6. Compute a ratio of two aggregates
level="error" | stats count() AS errors, distinct_count(host) AS hosts | eval per_host = errors / hostsAn eval after stats works on the aggregate's own columns.