Splunk Search

count based on time range

harish_ka
Communicator

I have search result of last 10 days.
Can we get the count based on time range, like "count(Alert) as Total count where timestamp=CurrentDate-5" (to get count of last 5 days).
I have to get the count of last 7 days,last 3 days from the same search result.
Now i am using "Join" with earliest and latest for each and every column like count of last 7 days,last 3 days etc..
the size of query is getting bigger when i use join. Any other way to get this result??

Any idea how to implement this...

Tags (2)

wpreston
Motivator

You could try using eval to get this. A day is 86,400 seconds, so 7 days would be 604,800. 3 days would be 259,200 seconds. So maybe something like this:

... your search here... 
| eval timeDiff=now() - _time 
| stats count(eval(timeDiff < 604801 AND timeDiff > 259200)) as Last7Days count(eval(timeDiff < 259201)) as Last3Days

wpreston
Motivator

If Book is a field in your data, just can just add it into the eval statement. As for evaluating a number of days worth of records, just multiply 86,400 by the number of days you need and make eval statement similar to what I've written above. So for your specific case:

5 days = 86,400 * 5 =
7 days = 86,400 * 7 = 604,800
10 days = 86,400 * 10 = 864,000

So your search will look like this:

... your search here... 
| eval timeDiff=now() - _time 
| stats stats count(eval(timeDiff < 432001 AND Book=*)) as Last5Days count(eval(timeDiff < 604801 AND Book=*)) as Last7Days count(eval(timeDiff < 864001 AND Book=*)) as Last10Days

In this search, the counts are inclusive of each other, meaning that the count for the last 7 days includes the books from the count of the last 5 days, and the count of the last 10 days includes the books from the last 7 days and last 5 days.

0 Karma

harish_ka
Communicator

Thanks Wpreston for your answer,

I need to get count of books.. it is like stats count(Book).
So how can i use the above query to get count for last 10,7,5 days...
Can you help me with the syntax...

0 Karma

harish_ka
Communicator

Can anyone help me on this?? its urgent...

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

How Splunk AI Assistant Is Transforming Investigations: Insights from .conf26

At .conf26 in Denver, I spent much of my time in conversation with Splunk customers about Splunk AI Assistant. ...

Make Your Machine Data AI-Ready in Splunk

Every team is getting ready for AI agents.   Thursday, November 12, 2026  |  11AM  PST / 2PM EST   ...

Splunk Asynchronous Forwarding Explained

Splunk asynchronous forwarding is often misunderstood as simply setting autoLBVolume. That is not quite right. ...