Splunk Search

How to get latest events by specific field?

sjs
Path Finder

Hey folks,

 

I have a query as such 

 

.. | ID="*" AND STATUS="*" | table _time ID STATUS

 

 

Here is the result which I got

sjs_0-1674133380576.png

 

.. | ID="*" AND STATUS="*" | table _time ID STATUS

 

If you notice here that for the same ID, I got all the status which logged, but I would like to have the latest status here

 

I did try few things like

 

... | ID="*" AND STATUS="*" | stats latest(STATUS) by ID | table _time ID STATUS

 

 

Here is what I got

sjs_1-1674133590202.png

 

I did get the unique ID's but rest of the fields are getting as null.

 

Could you please help me here

Labels (1)
0 Karma
1 Solution

richgalloway
SplunkTrust
SplunkTrust

The _time and STATUS fields are null because they were discarded by the stats command.  Try this

... | ID="*" AND STATUS="*" 
| eventstats latest(STATUS) as latestStatus by ID 
| where STATUS==latestStatus
| table _time ID STATUS
---
If this reply helps you, Karma would be appreciated.

View solution in original post

richgalloway
SplunkTrust
SplunkTrust

The _time and STATUS fields are null because they were discarded by the stats command.  Try this

... | ID="*" AND STATUS="*" 
| eventstats latest(STATUS) as latestStatus by ID 
| where STATUS==latestStatus
| table _time ID STATUS
---
If this reply helps you, Karma would be appreciated.

gcusello
SplunkTrust
SplunkTrust

Hi @sjs,

after a stats command you have only the fields that you used in the command, so you have to declare all the field you want, something like this:

| stats 
   latest(_time) AS _time 
   last(STATUS) AS STATUS 
   BY ID 
| table _time ID STATUS

Ciao.

Giuseppe

Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.
Get Updates on the Splunk Community!

Tech Talk Recap | Mastering Threat Hunting

Mastering Threat HuntingDive into the world of threat hunting, exploring the key differences between ...

Observability for AI Applications: Troubleshooting Latency

If you’re working with proprietary company data, you’re probably going to have a locally hosted LLM or many ...

Splunk AI Assistant for SPL vs. ChatGPT: Which One is Better?

In the age of AI, every tool promises to make our lives easier. From summarizing content to writing code, ...