Splunk Search

Events with maximum value of dynamic field

mmclain1
Explorer

I think I'm missing something. I have rex generating a new field for me. I want to return only events with the maximum value for that field. Let's say the field is called testfield and the possible values for that field are 1, 2, or 3. I want to return only events with testfield=3. However, this will be in a dashboard and the next time it runs, the max value could be 4. How do I do this?

Tags (3)
1 Solution

sideview
SplunkTrust
SplunkTrust

Here's something that should work for you.

<your searchterms> | eventstats  max(testfield) as max | where testfield=max | fields - max

eventstats works much like its bigger brother stats, except that it paints its statistics back onto the original rows that were input, rather than aggregating/transforming the set. This generally means that it puts its stats back on the 'events' (although not necessarily - you can get good use out of eventstats further down the search pipeline sometimes too)

And if you're not actually interested in just the raw event text, but rather in one or more fields that are on those events, then a more straightforward way might be to do this:

<your searchterms> | stats max(someField) values(someOtherField) avg(someThirdField) by testField | sort - testField | head 1

View solution in original post

sideview
SplunkTrust
SplunkTrust

Here's something that should work for you.

<your searchterms> | eventstats  max(testfield) as max | where testfield=max | fields - max

eventstats works much like its bigger brother stats, except that it paints its statistics back onto the original rows that were input, rather than aggregating/transforming the set. This generally means that it puts its stats back on the 'events' (although not necessarily - you can get good use out of eventstats further down the search pipeline sometimes too)

And if you're not actually interested in just the raw event text, but rather in one or more fields that are on those events, then a more straightforward way might be to do this:

<your searchterms> | stats max(someField) values(someOtherField) avg(someThirdField) by testField | sort - testField | head 1

mmclain1
Explorer

Perfect, that's exactly what I was trying to figure out how to do! Thanks!

0 Karma

rodrigo_santos
Explorer

Great... 😄

0 Karma
Get Updates on the Splunk Community!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

March 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...