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!

Good Sourcetype Naming

When it comes to getting data in, one of the earliest decisions made is what to use as a sourcetype. Often, ...

See your relevant APM services, dashboards, and alerts in one place with the updated ...

As a Splunk Observability user, you have a lot of data you have to manage, prioritize, and troubleshoot on a ...

Splunk App for Anomaly Detection End of Life Announcement

Q: What is happening to the Splunk App for Anomaly Detection?A: Splunk is officially announcing the ...