Knowledge Management

How do I subtract two search?

politrons
Explorer

I´m making a union of two search, and now I´m trying to make a subtract of the two variables.

 

| set union [search "Output MeshRequestId" | stats avg(time) as avgTimeOut ] [search "Input MeshRequestId" | stats avg(time) as avgTimeInt ]  |stats count(eval(diff=avgTimeOut-avgTimeInt)) as TimeInThrottler

 

Everything fine before the latest count. I can see the definition of two avg variables.

But when I do the stats count(eval(diff=avgTimeOut-avgTimeInt)) as TimeInThrottler

Always return 0. Any idea what is wrong

Labels (2)
0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

union is producing 2 events, one with avgTimeOut and one with avgTimeInt - the calculation is working on one event at a time from the pipeline, so for each event, one of the fields is null.

Have you considered using appendcols in this scenario?

0 Karma

politrons
Explorer

Thanks for the answer, any example that I can refer of how it should be done using appendcols for this use case?

 

regards 

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Try something like this

search "Output MeshRequestId" | stats avg(time) as avgTimeOut 
| appendcols [search "Input MeshRequestId" | stats avg(time) as avgTimeInt ]  | stats count(eval(diff=avgTimeOut-avgTimeInt)) as TimeInThrottler

FrankVl
Ultra Champion

At that point you could also simply replace that last stats by an eval. (Unless I'm somehow completely missing what you're trying to achieve with that stats)

 

search "Output MeshRequestId"
| stats avg(time) as avgTimeOut 
| appendcols [search "Input MeshRequestId" | stats avg(time) as avgTimeInt ]  | eval TimeInThrottler = avgTimeOut - avgTimeInt

 

 

0 Karma

politrons
Explorer

Thanks for all the answers, after apply both option of union and append. I got same result

Only avgTimeInt is present, and I cannot show the total Difference.

Also I would like to use the result in stats or timechar if is possible

 

splunk_issues.PNG

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

It looks like

"Output MeshRequestId"
| stats avg(time) as avgTimeOut 

is not producing any results - please check the events from the search to make sure they have the time field with that exact name.

politrons
Explorer

Yep silly me the query were wrong, thanks  lot!

0 Karma
Get Updates on the Splunk Community!

.conf25 Community Recap

Hello Splunkers, And just like that, .conf25 is in the books! What an incredible few days — full of learning, ...

Splunk App Developers | .conf25 Recap & What’s Next

If you stopped by the Builder Bar at .conf25 this year, thank you! The retro tech beer garden vibes were ...

Congratulations to the 2025-2026 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...