Splunk Search

Aggregate only some results

PickleRick
SplunkTrust
SplunkTrust

I'll probably find my solution finally but if someone has something at hand, I'd be grateful for sharing 🙂

I have some results. Let's say they are like this:

CountFieldAFieldB
11a 
12b 
34c1
54d1
462e 
0f3
12g3
4h3

 

I would like the values from the count column summed up but only for the events that have FieldB defined. For the rest, I want them lest split by FieldA. For those summed up I want the FieldA to be aggregated into a multivalue field

So effectively the output should be like

CountFieldAFieldB
11a 
12b 
88c
d
1
462e 
16f
g
h
3

 

OK. I think I can get it done by adding another column being created conditionally either from fieldA or fieldB, then aggregating by this field. Something like this:

<initial search>
| eval tempfield=if(isnull(fieldB),"fieldA-".fieldA,"fieldB-".fieldB)
| stats sum(count) as count values(fieldA) as fieldA values(fieldB) as fieldB by tempfield
| fields - tempfield

Any nicer way?

Labels (2)
0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Not sure if this is nicer, but would this work?

| eventstats sum(Count) as AllCount values(FieldA) as AllFieldA by FieldB
| eval Count=if(isnull(FieldB),Count,AllCount)
| eval FieldA=if(isnull(FieldB),FieldA,AllFieldA)
| dedup FieldA
| table Count FieldA FieldB

PickleRick
SplunkTrust
SplunkTrust

Seems to produce the same results  although no offence but I since those temporary fields are getting quite huge I'd not call that nicer 😉

But it's an interesting approach. I keep forgetting about eventstats. Thanks for the insight!

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Your Feedback. Our Roadmap. Visit the PX Feedback Booth at .conf26

You use Splunk every day, come and help shape what's next.  Save Your Seat: Product-Focused Sessions at ...

Agentic SOC Triage: Investigating Splunk ES Notables with MCP Server and a Local LLM

The Problem: Too Many Alerts, Too Little Context Security operations teams running Splunk Enterprise Security ...

Painting a Clearer Picture: Creating Cross-Domain Visibility with AI Canvas

Watch Now Painting a Clearer Picture: Creating Cross-Domain Visibility with AI Canvas     Do you ever feel ...