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.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Community Content Calendar, September edition

Welcome to another insightful post from our Community Content Calendar! We're thrilled to continue bringing ...

Splunkbase Unveils New App Listing Management Public Preview

Splunkbase Unveils New App Listing Management Public PreviewWe're thrilled to announce the public preview of ...

Leveraging Automated Threat Analysis Across the Splunk Ecosystem

Are you leveraging automation to its fullest potential in your threat detection strategy?Our upcoming Security ...