Skip to main content
Version: 5.2

stats

Description

Performs statistical operations on data.

info

The stats command supports a mode for handling large volumes of data without requiring additional memory.

Syntax

stats <functions-expression> ["," <functions-expression>] [<by_expression>]

Required Arguments

At least one of the following functions is required:

ParameterSyntaxDescription
countcount | count(<field>)Calculates the number of events containing the field. If no field is specified, it counts the total number of events.
valuesvalues(<field>)Calculates an array of unique values in a given field.
avgavg(<field>)Calculates the average value in a given field.
dcdc(<field>)Calculates the number of unique values in a given field.
earliestearliest(<field>)Returns the field's value for the earliest event. An optional second parameter specifies the timestamp field name; the default is @timestamp.
firstfirst(<field>)Returns the first value in a given field.
lastlast(<field>)Returns the last value in a given field.
latestlatest(<field>)Returns the field's value for the latest event. An optional second parameter specifies the timestamp field name; the default is @timestamp.
listlist(<field>)Calculates an array of all values in a given field.
maxmax(<field>)Returns the maximum value in a given field.
minmin(<field>)Returns the minimum value in a given field.
rangerange(<field>)Calculates the difference between the maximum and minimum values in a given field.
stdevstdev(<field>)Calculates the standard deviation in a given field.
sumsum(<field>)Calculates the sum of values in a given field.

Optional Arguments

ParameterSyntaxDefaultDescription
<by_expression><by_field> ["," <by_field> ...]The field name(s) for grouping values.

Query Examples

Basic Examples

Example 1

Calculate the maximum processor temperature for each host.

source apc_snmp
| stats max(cpu_temperature) as 'Maximum processor temperature' by host

In this example, the maximum value for the field cpu_processor_temperature is calculated for each unique host and the result is stored in the field 'Maximum processor temperature'.

The result of the query might be the following table:

hostMaximum processor temperature
BJS-WS-0187
DXB-DB-0258
SIN-WS-0346

Example 2

Calculate the number of requests and the list of methods used for each client and request.

source apache-*
| stats count as 'Number of Requests', values(method) as 'Used Methods' by user, path
| sort - 'Number of Requests'

In this example, the system calculates the number of requests and the list of methods used for each unique pair of user and path. Then, the results are sorted by 'Number of Requests'.

The result of the query might be the following table:

userpathNumber of RequestsUsed Methods
SmithMG/posts8GET
POST
WadsonSI/about4GET
WrightAE/hot-chick.png3GET

Advanced Examples

Example 1

In this example, the system returns a list of all unique values in the user field:

... | stats values(user)

Sample input data:

usersource.addressevent
SmithMG192.168.1.10login
WadsonSI192.168.1.15logout
SmithMG192.168.1.20login
WrightAE10.0.0.1password_reset
HolmesVA10.0.0.5password_reset

The query execution result may be the following table:

values (user)
SmithMG
WadsonSI
v
HolmesVA

Example 2

In this example, the event field in the data has values start and end. After running this command, the system returns pairs of start with a list of unique user values (where event = "start") and end with a list of unique user values (where event = "end").

... | stats values(user) by event

Example 2.2

In this example, the response.status field in the data contains HTTP status values. After executing the command, the system will return pairs: 200 and a list of unique source.address values (where response.status = "200"), 403 and a list of unique source.address values (where response.status = "403"), and similarly for response.status = "401":

... | stats values(source.address) by response.status

Sample input data:

userresponse.statussource.addressrequest.referrernetwork.bytes
SmithMG200192.168.1.10/data2048
WadsonSI200192.168.1.15/login1024
SmithMG403175.100.1.20/data512
WrightAE40110.0.0.1/config128

The query execution result may be the following table:

response.statusvalues (source.address)
200192.168.1.10
192.168.1.15
403175.100.1.20
40110.0.0.1

Example 3

In this example, the system shows a list of all unique values in the user and message fields:

... | stats values(user), values(message)

The following examples will use input data from user action audits, a portion of which is presented below:

l_timehostusertargetactionoutcome
2025-05-30 13:47:08ws-01smithwadsonlocked-out-user-account
changed-password
added-member-to-group
success
2025-05-30 13:47:08ws-01avdeevwadsonreset-password
changed-password
failure
2025-05-30 13:47:08ws-01wrightholmesenabled-user-accountsuccess
2025-05-30 13:47:08ws-02taylorjohnchanged-passwordfailure
2025-05-30 13:47:08ws-01wrightjechremoved-member-from-groupsuccess
2025-05-30 13:47:08ws-01smithtayloradded-member-to-groupfailure
2025-05-30 13:47:08ws-02сooperfordremoved-member-from-groupfailure

Example 3.1

In this example, the system will display a list of all unique values of the user and action fields:

... | stats values(user), values(action)

The query execution result may be the following table:

values (user)values (action)
smith
wadson
wright
taylor
сooper
locked-out-user-account
reset-password
enabled-user-account
changed-password
removed-member-from-group
added-member-to-group

Example 3.2

In this example, the system will return a list of unique user and action values that share the same combination of user and action field values:

... | stats values(user), values(action) by user, action

The query execution result may be the following table:

useractionvalues (user)values (action)
smithlocked-out-user-accountsmithlocked-out-user-account
changed-password
added-member-to-group
smithchanged-passwordsmithlocked-out-user-account
changed-password
added-member-to-group
smithadded-member-to-groupsmithlocked-out-user-account
changed-password
added-member-to-group
wadsonreset-passwordwadsonreset-password
changed-password
wadsonchanged-passwordwadsonreset-password
changed-password
wrightenabled-user-accountwrightenabled-user-account
taylorchanged-passwordtaylorchanged-password
wrightremoved-member-from-groupwrightremoved-member-from-group
smithadded-member-to-groupsmithadded-member-to-group
сooperremoved-member-from-groupсooperremoved-member-from-group

Example 3.3

In this example, the system will return the number of documents containing the user field:

... | stats count(user)

The query execution result may be the following table:

count (user)
5

Example 3.4

In this example, the system will return the number of documents containing the action field for each unique value of the user field:

... | stats count(action) by user

The query execution result may be the following table:

usercount (user)
smith955
wadson34
wright1007
taylor1022
сooper873

Example 3.5

In this example, the system will return the number of documents containing the user field and the number of documents containing the action field:

... | stats count(user), count(action)

The query execution result may be the following table:

count (user)count (action)
53891

Example 3.6

In this example, the system will return the number of documents containing the user field for each unique combination of user and action field values:

... | stats count(user) by user, action

The query execution result may be the following table showing the first 7 entries:

useractioncount (user)
smithlocked-out-user-account201
smithchanged-password40
smithadded-member-to-group46
wadsonlocked-out-user-account271
wrightlocked-out-user-account511
smithdisabled-user-account23
tayloradded-user-account51
сooperreset-password1
сooperchanged-password2
wrightadded-user-account59

Example 3.7

In this example, the system will return the contents of the action field for the first record in the sample, the contents of the action field for the last record in the sample, and the average value of the action_count field (which was calculated by the command from the previous example) for each unique user:

... | stats count(user) as action_count by user, action
| stats first(action), last(action), avg(action_count) by user

The query execution result may be the following table:

userfirst (action)last (action)avg (action_count)
smithdeleted-user-accountchanged-password95.8
wadsonlocked-out-user-accountchanged-password100.1
wrightremoved-member-from-groupreset-password102.2
taylorremoved-member-from-groupreset-password113.22
сooperchanged-passwordreset-password1.5

Example 3.8

In this example, the system will return the contents of the action field for the earliest record in the sample and the contents of the user field for the latest record in the sample:

... | stats earliest(action), latest(user) by @timestamp

The query execution result may be the following table showing the first 7 entries:

@timestampearliest (action)latest (user)
2025-05-30T10:20:00.000000+0000removed-member-from-groupsmith
2025-05-30T10:21:00.000000+0000added-member-to-groupsmith
2025-05-30T10:22:00.000000+0000removed-member-from-groupwadson
2025-05-30T10:23:00.000000+0000deleted-user-accountwright
2025-05-30T10:24:00.000000+0000enabled-user-accountsmith
2025-05-30T10:25:00.000000+0000locked-out-user-accountsmith
2025-05-30T10:26:00.000000+0000unlocked-user-accountсooper

Example 4

Advanced example of using earliest and latest:

In this example, the system will extract the earliest and latest event by their indexing time indextime, after slightly cleaning the format of this time.

source radius_logs | sort indextime | eval indextime=substr(indextime, 1, (len(indextime) - 1) ) | stats latest(event, indextime), earliest(event, indextime)

Sample input data:

indextimeusernamenas_ipacct_status_typeevent
2025-04-30T10:15:00Zsmith192.168.100.1Access-RejectAuthentication Failed
2025-04-30T09:55:00Zwadson192.168.100.15Interim-UpdateAccounting Interim
2025-04-30T10:45:00Zsmith192.168.100.1StartAuthentication Success
2025-04-30T10:30:00Zсooper10.10.0.23StopAccounting Stop

The query execution result may be the following table:

latest (event, indextime)earliest (event, indextime)
Session timeoutUser login failed

Example 5

In this example, the system will return the number of unique values in the user field:

... | stats dc(user)

The query execution result may be the following table:

userdc (user)
smith
wadson
smith
сooper
3

Example 6

In this example, the system will return a list of all values of the user and items fields:

Collects all values of the specified field within a single group as an array, including duplicates.

... | stats list(user), list(items)

Sample input data:

useroperation_statustotal_priceitems
smithfailure1657Chicken strips
Fried squid rings
Pepper sausages
wadsonsuccess229Chicken tacos
smithsuccess1198Chicken tacos
Chicken teriyaki
сooperfailure1198Gyros

The query execution result may be the following table:

list (user)list (items)
smith
wadson
smith
сooper
Chicken strips
Fried squid rings
Pepper sausages
Chicken tacos
Chicken tacos
Chicken teriyaki
Gyros

Example 7

Example 7.1

In this example, the system will return the standard deviation of the log.offset field:

... | stats stdev(log.offset)

7.2

Analysis of CPU load host.cpu.usage for each host.ip field.

In this example, the system will return the event count count, the range between maximum and minimum values, average CPU load host.cpu.usage, standard deviation, and variance by host IP addresses host.ip:

... | stats count,
range(host.cpu.usage) as 'CPU Usage Range',
avg(host.cpu.usage) as 'CPU Usage Average',
stdev(host.cpu.usage) as 'Standard Deviation' by host.ip
| eval var=pow('Standard Deviation',2)
| rename var as 'Variance'

Sample input data:

host.iphost.os.namesystem.cpu.coreshost.cpu.usage
172.100.40.10Windows Server 2012 R2 Standard20.0581
172.95.150.218Windows Server 2016 Standard20.1611
172.255.215.11Ubuntu20.2011
172.100.40.10CentOS Linux20.0663
172.255.215.35Debian GNU/Linux20.295

The query execution result may be the following table:

host.ipcountCPU Usage RangeCPU Usage AverageStandard DeviationVariance
172.100.40.10280.180120.174350.06939461434434230.004815612499999998
172.95.150.218270.26920.163960.109919131890474630.01208221555555556
172.255.215.11180.06860.280960.0282586073416382050.0007985488888888887
172.100.40.10150.25080.21650.117946795915220460.01391144666666667
172.255.215.35130.036690.13420.01523439091879510.0002320866666666666

Example 8

Example 8.1

In this example, the system will return the value of the user field that occurs in 30 percent of cases:

... | stats perc(user, 30)

Example 8.2

Analysis of pollutant concentration values measurement.value by pollutant type event.category:

... | stats list(measurement.value), 
avg(measurement.value),
median(measurement.value),
perc(measurement.value, 90) by event.category

The query execution result may be the following table:

event.categorylist (measurement.value)avg (measurement.value)median (measurement.value)perc (measurement.value, 90)
PM2.515.5
17.9
16.3
18.0
16.92517.117.94
PM1027.5
26.3
25.0
30.0
27.226.929.7
NO240.0
43.8
41.1
45.0
42.47542.4544.82