Splunk Search

How to use stats with condition from multiple fields?

Utkc137
Explorer

Greetings,

 

I am struggling with creating a table in splunk which would do the following transformation:

Find the discrete count of id for A, B, and C where value the is 1, by month. 

tt_3.png

Currently, I am calculating values for each column individually using eventstats and combining the results.

However, we have a lot of columns (a,b,c,d.....) and thus the SLP does not preform efficiently. 

 

Looking for a more efficient approach to this.

 

Thanks in advance!

Labels (1)
0 Karma
1 Solution

Thulasinathan_M
Contributor

Hi,
Please try below:

| stats max(A) as ACnt, max(B) as BCnt, max(C) as CCnt by month, id
| stats sum(ACnt) as ACnt, sum(BCnt) as BCnt, sum(CCnt) as CCnt by month

 

View solution in original post

Thulasinathan_M
Contributor

Hi,
Please try below:

| stats max(A) as ACnt, max(B) as BCnt, max(C) as CCnt by month, id
| stats sum(ACnt) as ACnt, sum(BCnt) as BCnt, sum(CCnt) as CCnt by month

 

Utkc137
Explorer

Hey thanks a ton!
Been breaking my head on this issue. 

0 Karma

Thulasinathan_M
Contributor

Happy that worked for you!! Happy Splunking 🙂

0 Karma

Thulasinathan_M
Contributor

Hi,
Are A,B,C,D fields?

0 Karma

Utkc137
Explorer

Yes, they are individual field values. 

0 Karma

gcusello
SplunkTrust
SplunkTrust

hi @Utkc137,

if the file name having values A, B and C is "id_cnt", you could use the chart command:

<your_search>
| chart count OVER month BY id_cnt

Ciao.

Giuseppe

0 Karma

Utkc137
Explorer

This isn't the case, all columns are individual fields values.

The original log is of the following format:

<date_time> <month> <id> A=1 B=0 C=0 ...

 

0 Karma

gcusello
SplunkTrust
SplunkTrust

Hi @Utkc137 ,

you could try stats with eval:

<your_search>
| stats 
   count(eval(A=1)) AS A_id_cnt 
   count(eval(B=1)) AS B_id_cnt 
   count(eval(C=1)) AS C_id_cnt 
   BY month

if it doesn't run, please try also:

<your_search>
| stats 
   count(eval(A="1")) AS A_id_cnt 
   count(eval(B="1")) AS B_id_cnt 
   count(eval(C="1")) AS C_id_cnt 
   BY month

Ciao.

Giuseppe

Utkc137
Explorer

The idea is to find discrete count of id's where A=1, B=1. Not the count of events where  these values are 1.

0 Karma

gcusello
SplunkTrust
SplunkTrust

HI @Utkc137,

good for you, see next time!

Ciao and happy splunking

Giuseppe

P.S.: Karma Points are appreciated by all the contributors 😉

gcusello
SplunkTrust
SplunkTrust

Hi @Utkc137.

sorry, what's the difference?

Ciao.

Giuseppe

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!

May 2026 Splunk Expert Sessions: Security & Observability

Level Up Your Operations: May 2026 Splunk Expert Sessions Whether you are refining your security posture or ...

Network to App: Observability Unlocked [May & June Series]

In today’s digital landscape, your environment is no longer confined to the data center. It spans complex ...

SPL2 Deep Dives, AppDynamics Integrations, SAML Made Simple and Much More on Splunk ...

Splunk Lantern is Splunk’s customer success center that provides practical guidance from Splunk experts on key ...