Splunk Search

Stats command with latest values listing

jerinvarghese
Communicator

Hi Team,

 

I have a splunk query that am testing for Service Now data extract.

 

index=snow "INC783"
| search dv_state="In Progress" OR dv_state="New" OR dv_state="On Hold"
| stats max(_time) as Time latest(dv_state) as State by number, dv_priority
| fieldformat Time=strftime(Time,"%Y-%m-%d %H:%M:%S")
| table number,Time, dv_priority, State

 

 

The challenge with the code is, above output is listing all the states for the particular Incidnet, even when i tried to filter for only the latest and max time.

numberTimedv_priorityState
INC7832024-11-13 16:56:141 - CriticalIn Progress
INC7832024-11-13 17:00:033 - ModerateOn Hold

 

The data must only show the latest one, which must be the one with "On Hold".
Tried multiple method, but failing and showing all.
how can i achieve it.

 

thanks

Jerin V

Labels (3)
0 Karma
1 Solution

ITWhisperer
SplunkTrust
SplunkTrust

Your by clause also include dv_priority which is why you are getting multiple results for an incident. Try something like this

index=snow "INC783"
| search dv_state="In Progress" OR dv_state="New" OR dv_state="On Hold"
| stats max(_time) as Time latest(dv_state) as State latest(dv_priority) as Priority by number
| fieldformat Time=strftime(Time,"%Y-%m-%d %H:%M:%S")
| table number,Time, Priority, State

View solution in original post

ITWhisperer
SplunkTrust
SplunkTrust

Your by clause also include dv_priority which is why you are getting multiple results for an incident. Try something like this

index=snow "INC783"
| search dv_state="In Progress" OR dv_state="New" OR dv_state="On Hold"
| stats max(_time) as Time latest(dv_state) as State latest(dv_priority) as Priority by number
| fieldformat Time=strftime(Time,"%Y-%m-%d %H:%M:%S")
| table number,Time, Priority, State
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!

Agent Mode Engaged! Enchaining Agentic Operations with Splunk AI Assistant 2.0

    Are you ready to transform how your team handles complex data requests? We invite you to our upcoming ...

Announcing Modern Navigation: A New Era of Splunk User Experience

We are excited to introduce the Modern Navigation feature in the Splunk Platform, available to both cloud and ...

Modernize your Splunk Apps – Introducing Python 3.13 in Splunk

We are excited to announce that the upcoming releases of Splunk Enterprise 10.2.x and Splunk Cloud Platform ...