Using stats to aggregate values

While top is very convenient, stats is extremely versatile. The basic structure of a stats statement is:

stats functions by fields 

Many of the functions available in stats mimic similar functions in SQL or Excel, but there are many functions unique to Splunk too. The simplest stats function is count. Given the following query, the results will contain exactly one row, with a value for the field count:

sourcetype=tm1* error | stats count

Using the by clause, stats will produce one row per unique value for each field listed, which is similar to the behavior of top. Run the following query:

sourcetype=tm1* error | stats count by date_month date_wday

It will produce a table like this:

There are a few things to ...

Get Implementing Splunk 7 - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.