Splunk Search

how to find difference between two "stats count" used in two different saved search

snabi
Explorer

So i have two saved search queries
1. sourcetype="x" "attempted" source="y" | stats count
2. sourcetype="x" "Failed" source="y" | stats count

i need to create a search query which will calculate

Passed item = (sourcetype="x" "attempted" source="y" | stats count) - (sourcetype="x" "Failed" source="y" | stats count)

and display Passed item count by hours

Tags (1)

sideview
SplunkTrust
SplunkTrust

sourcetype="x" source="y" (attempted OR failed) | eval isAttempted=if(searchmatch("attempted"),1,0) | eval isFailed=if(searchmatch("failed"),1,0) | stats sum(isAttempted) as attempts sum(isFailed) as failures | eval successes=attempts-failures

However I would examine the matching events for the "attempted" and "failed" searches very carefully to maked sure there aren't any false positives or negatives. Possibly this is already a boiled-down example. Note that you can have very complex boolean logic in the (attempted OR failed) side, and you can have the correspondingly complex boolean logic embedded into the searchmatch() functions so you'll be fine even if your actual case is more complex.

UPDATE:

If using eval syntax right in the stats command is more your style, that can be done too and it becomes a lot more compact:

sourcetype="x" source="y" (attempted OR failed) | stats count(eval(searchmatch("attempted"))) as attempts count(eval(searchmatch("failed"))) as failures | eval successes=attempts-failures

lukejadamec
Super Champion

It looks like you're trying to subtract apples from oranges, which cannot even be done on paper let alone splunk.
If you're trying to remove a subset of a set and the remaining items by hours, then you don't want to use stat counts. Just use a search that excludes them like this:
sourcetype="x" source="y" "attempted" | search NOT (sourcetype="x" source="y" "Failed") | stats count by _time,passeditem

Get Updates on the Splunk Community!

Introducing the 2024 SplunkTrust!

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

Introducing the 2024 Splunk MVPs!

We are excited to announce the 2024 cohort of the Splunk MVP program. Splunk MVPs are passionate members of ...

Splunk Custom Visualizations App End of Life

The Splunk Custom Visualizations apps End of Life for SimpleXML will reach end of support on Dec 21, 2024, ...