Skip to content
SPL // Splunk

Using the stats Command

KGI Avatar
 

Written by: Kinney Group | Last Updated:

 
June 4, 2025
 
Search Command Of The Week: stats
 
 

Originally Published:

 
October 9, 2024

Splunk’s Search Processing Language (SPL) is a powerful tool for extracting meaningful insights from large volumes of machine data. It allows users to filter, transform, and analyze data to gain real-time visibility into their environment. Once you have your raw data, it’s essential to be able to aggregate, summarize, and manipulate it to uncover patterns and trends. That’s where the stats command comes in. It’s one of the most important and widely used commands in SPL for performing calculations and statistical operations on your data. 

Using the stats Command

The stats command lets you calculate statistical metrics based on the values of fields in events. This command is especially useful when you need to analyze large datasets and derive meaningful insights from them. 

For example, you can use stats to count events, calculate averages, sums, standard deviations, and minimum/maximum values. It’s a versatile command that can transform your data, making it easier to summarize data and create useful visualizations in Splunk. 

Proper Syntax

The stats command syntax is relatively simple and follows this basic structure: 

				
					| stats [statistical function] (field) AS new_field_name BY <grouping_field> 
				
			

Here’s a breakdown of the key components: 

  • Statistical function Functions like count, sum, avg, min, max, etc., that aggregate data. 
  • Field The specific field you want to calculate the function on. 
  • new_field_name Optional alias for the result of your calculation. 
  • Grouping field Optional field to group your results by.

For example, | stats avg(bytes) BY host calculates the average for the bytes field for each host. 

Benefits of Using the stats Command

The stats command provides several benefits for users working with Splunk data, especially when analyzing trends or summarizing large datasets: 

  • Data Summarization: Summarize your data by aggregating values, making it easier to find patterns. 
  • Improved Performance: Aggregating data with stats can help reduce the amount of data that needs to be processed, leading to faster searches. 
  • Flexible Analysis: With a wide range of statistical functions from simple counts to advanced calculations like percentiles and standard deviations. 
 

Usage of the stats Command

Let’s explore some practical examples of using the stats command in your everyday Splunk activities. 

EXAMPLE #1: Counting Events by Source

You need to know how many events have been logged for each data source. This can help you monitor the distribution of incoming data across various sources. 

				
					| stats count BY source 
				
			

This query is useful for administrators who need to ensure that logs are being collected from all sources and to monitor data ingestion volumes. 

EXAMPLE #2: Summing Total Data Transfer

You want to calculate the total amount of data transferred by each user during a specific period. This can help you track data usage patterns and identify any users consuming large amounts of bandwidth. 

				
					| stats sum(data_transferred) AS total_data_transferred BY user 
				
			

This query is useful for network administrators who need to monitor data usage across users, detect high-bandwidth consumers, and manage network resources accordingly. It can also help with auditing for unusual activity related to excessive data transfers. 

EXAMPLE #3: Analyzing Maximum and Minimum Transaction Amounts

You want to find the highest and lowest transaction amounts from your e-commerce data to detect possible anomalies or large orders. 

				
					| stats min(amount) AS min_amount, max(amount) AS max_amount BY user 
				
			

This query is useful for business analysts who need to track user spending patterns and identify outliers or potentially fraudulent transactions. 

Conclusion

The stats command is a fundamental search in Splunk SPL, offering users the ability to perform a variety of statistical calculations on their data. Whether you’re summarizing event counts, calculating averages, or analyzing performance metrics, stats provides a flexible and efficient way to aggregate data.  

By incorporating stats into your searches, you’ll be better equipped to transform raw data into actionable insights in Splunk. 

To access more Splunk searches, check out Atlas Search Library, which is part of the Atlas Platform. Specifically, Atlas Search Library offers a curated list of optimized searches. These searches empower Splunk users without requiring SPL knowledge. Furthermore, you can create, customize, and maintain your own search library. By doing so, you ensure your users get the most from using Splunk.

Atlas Search Library
Helpful? Don't forget to share this post!
LinkedIn
Reddit
Email
Facebook