Splunk Search

How to make conditional stats aggregation query?

mxh7777
Path Finder

Hi,

I'm looking for how to make conditional stats aggregation query according to a form input "With users" (value : Yes or No)

I got a list of events per User

When form input With users is equal "Yes", i'd like to present such a table

User URI avgNb avgDuration
A A 1 1
A B 2 5
A C 3 1
B A 5 9
B C 6 10
C A 4 11
C B 6 8

 

Query : | stats count as avgNb avg(DUR) as avgDuration by USR

An when form input With users is equal "No", i'd like to present this one

URI avgNb avgDuration
A 3,33333333 7
B 4 6,5
C 4,5 5,5

Query : | stats count as avgNb avg(DUR) as avgDuration

 

How can I build my query according to this form input condition?

Thanks

Labels (1)
0 Karma
1 Solution

ITWhisperer
SplunkTrust
SplunkTrust

It looks like your queries are more like

| stats count as avgNb avg(DUR) as avgDuration by USR URI

and

| stats count as avgNb avg(DUR) as avgDuration by URI

Given that, you could set up the label and value of the input to be like this

LabelValue
YesUSR URI
NoURI

Then modify the query like this

| stats count as avgNb avg(DUR) as avgDuration by $user$

View solution in original post

ITWhisperer
SplunkTrust
SplunkTrust

It looks like your queries are more like

| stats count as avgNb avg(DUR) as avgDuration by USR URI

and

| stats count as avgNb avg(DUR) as avgDuration by URI

Given that, you could set up the label and value of the input to be like this

LabelValue
YesUSR URI
NoURI

Then modify the query like this

| stats count as avgNb avg(DUR) as avgDuration by $user$

mxh7777
Path Finder

HI @ITWhisperer 

Thanks for answering.

I tried this just to see if it works but it doesn't work

| eval label="USR URI"
| stats count as avgNb avg(DUR) as avgDuration by $label$

=> No results return

whereas this works

| stats count as avgNb avg(DUR) as avgDuration by USR URI

 

Something wrong in my eval instruction ?

Thanks

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Yes, the label and value are from your form input (which you have referenced but not shown) not an eval

mxh7777
Path Finder

@ITWhisperer 

It works perfectly !!

Thanks a lot

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 ...