Splunk Search

How can I do a sum of count for if statement?

tamduong16
Contributor

When I do a stats count by a specific column. The count for each of them work. Here is the picture:

alt text

But when I actually try to count only the Failed Attempt call. The result is 0. Did I do something wrong here?

alt text

0 Karma
1 Solution

somesoni2
SplunkTrust
SplunkTrust

In the eval command expressions (and where command too), if a field name contains spaces, you need to enclose them in single quotes not double quotes. With double quotes, they are treated as literal string instead of fields. You did it correctly in line 2 (replace command) but missed in line three. Correcting that would resolve your issue.

Like this

index="monthlycdr"
| eval "Disconnect Info"=replace('Disconnect Info' ,"\"","")
| eval "Failure Rate" = if(match('Disconnect Info',"(?i)Failed Attempt"),1,0)
| stats sum("Failure Rate")

View solution in original post

cmerriman
Super Champion

can you avoid the spaces? SPL works better, generally, without them.

index=monthlycdr
|eval DisconnectInfo=replace('Disconnect Info',"\"","")
|eval "FailureRate"=if(match(DisconnectInfo,"(?i)Failed Attempt"),1,0)
|stats sum(FailureRate) as "Failure Rate"

somesoni2
SplunkTrust
SplunkTrust

In the eval command expressions (and where command too), if a field name contains spaces, you need to enclose them in single quotes not double quotes. With double quotes, they are treated as literal string instead of fields. You did it correctly in line 2 (replace command) but missed in line three. Correcting that would resolve your issue.

Like this

index="monthlycdr"
| eval "Disconnect Info"=replace('Disconnect Info' ,"\"","")
| eval "Failure Rate" = if(match('Disconnect Info',"(?i)Failed Attempt"),1,0)
| stats sum("Failure Rate")

niketn
Legend

I think following is the only query required. | eval "Disconnect Info"=replace('Disconnect Info' ,"\"","") before stats is required only if double quotes may be present between "Failed Attempt" pattern being matched.

 index="monthlycdr"
|  stats count(eval(match('Disconnect Info',"(?i)^Failed Attempt"))) as "Failed Attempt"
____________________________________________
| makeresults | eval message= "Happy Splunking!!!"
0 Karma

tamduong16
Contributor

thanks guys

0 Karma
Get Updates on the Splunk Community!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

March 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...