stats
table
The stats command performs aggregations (such as count, sum, avg, min, max, first, last) over your data, optionally grouped by one or more fields.
Syntax
stats [agg1(field1) as alias1, ...] [by groupField1, groupField2, ...]agg(field) as alias: Aggregation function (see below) applied to a field, optionally renamed withas.by groupField1, ...: (Optional) Group results by one or more fields.
Available Aggregation Functions
count(): Counts the number of records in each group (or overall if no grouping).sum(field): Sums the values offieldfor each group.avg(field): Calculates the average value offieldfor each group.min(field): Finds the minimum value offieldfor each group.max(field): Finds the maximum value offieldfor each group.first(field): Returns the first non-null value offieldin each group.last(field): Returns the last non-null value offieldin each group.
You can use multiple aggregation functions in a single
statscommand, and each can be given an alias usingas.
Usage
- Use
statsto summarize data, such as counting events or calculating averages. - Multiple aggregations and groupings are supported.
Example
stats count() as total, avg(duration) as avg_duration, first(user) as first_user by statusThis counts all records, calculates the average duration, and returns the first user for each status group.