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!

Thanks for the Memories! Splunk University, .conf25, and our Community

Thank you to everyone in the Splunk Community who joined us for .conf25, which kicked off with our iconic ...

Data Persistence in the OpenTelemetry Collector

This blog post is part of an ongoing series on OpenTelemetry. What happens if the OpenTelemetry collector ...

Introducing Splunk 10.0: Smarter, Faster, and More Powerful Than Ever

Now On Demand Whether you're managing complex deployments or looking to future-proof your data ...