Splunk Search

How to combine multiple stats distinct counts into one search?

raindrop18
Communicator

I have these 3 stats count searches, but I would like to combine them as one and create a table.

 host ="web*" source="Access" "men" | stats dc(id) by city
 host ="web*" source="Access" "women" | stats dc(id) by city
 host ="web*" source="Access" | stats dc(id) by city

My objective is to have the number for men, women, and total (men+women)

thanks,

0 Karma
1 Solution

woodcock
Esteemed Legend

Like this:

host ="web*" source="Access" | eval type = case(searchmatch("men"), "All,Men", searchmatch("women"), "All,Women", true(), "All,Neither") | makemv delim="," type | chart dc(id) over city BY type

View solution in original post

woodcock
Esteemed Legend

Like this:

host ="web*" source="Access" | eval type = case(searchmatch("men"), "All,Men", searchmatch("women"), "All,Women", true(), "All,Neither") | makemv delim="," type | chart dc(id) over city BY type

raindrop18
Communicator

thank you so much again, perfect outcome and really good lesson.

0 Karma

raindrop18
Communicator

thank you so very much, quick question, do you mind if you can explain to me what's the purpose of "Neither"? also the total count for (men+women) by each city rather than total (men/women total. sorry I think my question was wrong earlier.

0 Karma

woodcock
Esteemed Legend

OK, I tweaked my answer a bit. I realized that since we are using dc instead of count, that using addtotals is invalid. So I switched to creating a value for type called All that will allow us to dc all events. As far as "Neither", there may be some events that have neither "men" nor "women" so this captures those. It is quite possible that "Neither" will always be zero but it is good to check, just in case. Also, I had the 2 fields' ( city and type ) positions swapped so the table was built backwards. I fixed it and it should look like what you are desiring now.

0 Karma
Get Updates on the Splunk Community!

Introducing Splunk Enterprise 9.2

WATCH HERE! Watch this Tech Talk to learn about the latest features and enhancements shipped in the new Splunk ...

Adoption of RUM and APM at Splunk

    Unleash the power of Splunk Observability   Watch Now In this can't miss Tech Talk! The Splunk Growth ...

Routing logs with Splunk OTel Collector for Kubernetes

The Splunk Distribution of the OpenTelemetry (OTel) Collector is a product that provides a way to ingest ...