Splunk Search

When using "tstats count", how to display zero results if there are no counts to display?

jsh315
Engager

I need to use tstats vs stats for performance reasons. I would like tstats count to show 0 if there are no counts to display.

| tstats count where index="abc" by _time span=1h

This would return a table with _time (every hour) and count, like this:

_time                   count
2017-01-01 12:00        50
2017-01-01 13:00        62
2017-01-01 14:00        14

But if the count is zero, it doesn't return that row. How do I get this to display 0 count for that hour?

Thank you.

0 Karma
1 Solution

rjthibod
Champion

How about this

 | tstats count where index="abc" by _time span=1h | makecontinuous span=1h _time | fillnull value=0

View solution in original post

rjthibod
Champion

How about this

 | tstats count where index="abc" by _time span=1h | makecontinuous span=1h _time | fillnull value=0

jsh315
Engager

Did not work. Still getting empty rows for where count is zero.

0 Karma

rjthibod
Champion

Try this instead

  | tstats count where index="abc" by _time span=1h | timechart span=1h max(count) as count | fillnull value=0 count
0 Karma

jsh315
Engager

That worked. If you have time I would greatly appreciate the explanation of your query. Thank you so much.

0 Karma

rjthibod
Champion

timechart by default (unless you specify fixedrange=f) creates a row for each time bucket from the beginning of the search period until the end of the search period. So, the timechart creates all the necessary rows, and then fillnull puts a 0 in all empty row.

0 Karma

jsh315
Engager

Thanks for the response. Your solution works if there is at least one row that returned some count but if all the rows had zero count then I get "No results found.". Is there a solution to handle this case as well? Thanks a lot.

0 Karma

rjthibod
Champion

Yes, you can use append to include a dummy value that gets filtered out if there is real data.

| tstats count where index="abc" by _time span=1h 
| addtotals fieldname=Total
| append [|gentimes start=-1 | addinfo | table info_min_time | bin info_min_time as _time span=1h | eval count = 0 | eval Total = 0 | table _time count Total]
| where (Total = 0 OR count > 0)
| timechart span=1h max(count) as count
| fillnull value=0
0 Karma

jsh315
Engager

That worked. Thanks a lot!

0 Karma
Get Updates on the Splunk Community!

Stay Connected: Your Guide to January Tech Talks, Office Hours, and Webinars!

What are Community Office Hours? Community Office Hours is an interactive 60-minute Zoom series where ...

[Puzzles] Solve, Learn, Repeat: Reprocessing XML into Fixed-Length Events

This challenge was first posted on Slack #puzzles channelFor a previous puzzle, I needed a set of fixed-length ...

Data Management Digest – December 2025

Welcome to the December edition of Data Management Digest! As we continue our journey of data innovation, the ...