Splunk Search

How to create a conditional stats search based on a field value?

ronny_wang
Explorer

Hi,
I am trying to write a conditional stats command based on a field value.
So for example:
I have a field called stat_command

Name, No., stat_command         
Name1, 5, latest
Name2, 12, avg
Name3, 13, max

So for stat_command = latest, I want to run | stats latest(Number)
for stat_command = avg, I want to run | stats avg(Number)

Is there a way to do this in a search-efficient way without doing many appends?
I have tried using macros, map, and case statements but have not had much luck.

Thanks

0 Karma
1 Solution

ronny_wang
Explorer

Hi,

As i had to split by a number of fields, including the Name field, we were able to do the following:
| stats latest(Value) AS latest_value
avg(Value) AS avg_value
max(Value) AS max_value
min(Value) AS min_value
count(Value) AS count_value
BY Name

| eval value=case(stats_command="latest", latest_value,
stats_command="avg", avg_value,
stats_command="min", min_value,
stats_command="max", max_value,
stats_command="count", count_value,
1=1, Value)

View solution in original post

0 Karma

ronny_wang
Explorer

Hi,

As i had to split by a number of fields, including the Name field, we were able to do the following:
| stats latest(Value) AS latest_value
avg(Value) AS avg_value
max(Value) AS max_value
min(Value) AS min_value
count(Value) AS count_value
BY Name

| eval value=case(stats_command="latest", latest_value,
stats_command="avg", avg_value,
stats_command="min", min_value,
stats_command="max", max_value,
stats_command="count", count_value,
1=1, Value)
0 Karma

richgalloway
SplunkTrust
SplunkTrust

@ronny_wang If your problem is resolved, please accept an answer to help future readers.

---
If this reply helps you, Karma would be appreciated.
0 Karma

renjith_nair
Legend

@ronny_wang,

Does this approach work for you ?

    | eventstats latest(No) as _latest,avg(No) as _avg,max(No) as _Max
    | eval value=case(stat_command=="latest",_latest,stat_command=="avg",_avg,stat_command=="max",_Max)

Get all types of aggregate values using stats and select only the value based on your condition

---
What goes around comes around. If it helps, hit it with Karma 🙂

ronny_wang
Explorer

Hi, As i had to split by a number of fields, including the name field, I was able to solve this with the following

| stats latest(Value) AS latest_value 
avg(Value) AS avg_value
max(Value) AS max_value
min(Value) AS min_value
count(Value) AS count_value
latest(_time) AS event_time_epoch
BY Name
| eval metric_value=case(stats_command="latest", latest_value,
stats_command="avg", avg_value,
stats_command="min", min_value,
stats_command="max", max_value,
stats_command="count", count_value,
1=1, Value)
0 Karma
Get Updates on the Splunk Community!

Federated Search for Amazon S3 | Key Use Cases to Streamline Compliance Workflows

Modern business operations are supported by data compliance. As regulations evolve, organizations must ...

New Dates, New City: Save the Date for .conf25!

Wake up, babe! New .conf25 dates AND location just dropped!! That's right, this year, .conf25 is taking place ...

Introduction to Splunk Observability Cloud - Building a Resilient Hybrid Cloud

Introduction to Splunk Observability Cloud - Building a Resilient Hybrid Cloud  In today’s fast-paced digital ...