<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic doing sum with if condition in search query in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/doing-sum-with-if-condition-in-search-query/m-p/650534#M224903</link>
    <description>&lt;P&gt;Hi All!&lt;/P&gt;&lt;P&gt;I want to calculate the sum of failed and declined&amp;nbsp;&lt;/P&gt;&lt;P&gt;| eval Msg=if((Failure_Message=="200 Emv error " OR Failure_Message=="NoAcquirerFoundConfigured "),"Failed","Declined")&lt;/P&gt;&lt;P&gt;Now I want to calculate the sum of failed and declined in the next line. I am already doing stats count of other fields, so need to add this one with them. Here is the one I am working on, but the problem is its not giving the output for Failed and Declined -&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Here is my query-&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;index=idx-stores-pos sourcetype=GSTR:Adyen:log&lt;BR /&gt;| transaction host startswith="Transaction started" maxpause=90s&lt;BR /&gt;| search Failure OR Success&lt;BR /&gt;| eval Store= substr(host,1,7)&lt;BR /&gt;| eval Register= substr(host,8,2)&lt;BR /&gt;| rex field=_raw "AdyenPaymentResponse:.+\sResult\s:\s(?&amp;lt;Status&amp;gt;.+)"&lt;BR /&gt;| rex field=_raw "AdyenPaymentResponse:.+\sReason\s:\s(?&amp;lt;Failure_Message&amp;gt;.+)"&lt;BR /&gt;| rex field=_raw "AdyenPaymentResponse:.+\sMessage\s:\s(?&amp;lt;Error_Message&amp;gt;.+)\;"&lt;BR /&gt;| replace "* " with * in Error_Message Failure_Message&lt;BR /&gt;| eval Msg=if((Failure_Message=="200 Emv error " OR Failure_Message=="NoAcquirerFoundConfigured "),"Failed","Declined")&lt;BR /&gt;| stats count(eval(Status="Success")) AS Success_Count count(eval(Status="Failure")) AS Failure_Count sum(eval(Msg="Failed")) AS Failed sum(eval(Msg="Declined")) AS Declined By Store Register&lt;BR /&gt;| eval Total_Payment= Success_Count + Failure_Count&lt;BR /&gt;| table Store Register Success_Count Failure_Count Total_Payment Failed Declined&lt;/P&gt;</description>
    <pubDate>Fri, 14 Jul 2023 11:16:42 GMT</pubDate>
    <dc:creator>man03359</dc:creator>
    <dc:date>2023-07-14T11:16:42Z</dc:date>
    <item>
      <title>doing sum with if condition in search query</title>
      <link>https://community.splunk.com/t5/Splunk-Search/doing-sum-with-if-condition-in-search-query/m-p/650534#M224903</link>
      <description>&lt;P&gt;Hi All!&lt;/P&gt;&lt;P&gt;I want to calculate the sum of failed and declined&amp;nbsp;&lt;/P&gt;&lt;P&gt;| eval Msg=if((Failure_Message=="200 Emv error " OR Failure_Message=="NoAcquirerFoundConfigured "),"Failed","Declined")&lt;/P&gt;&lt;P&gt;Now I want to calculate the sum of failed and declined in the next line. I am already doing stats count of other fields, so need to add this one with them. Here is the one I am working on, but the problem is its not giving the output for Failed and Declined -&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Here is my query-&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;index=idx-stores-pos sourcetype=GSTR:Adyen:log&lt;BR /&gt;| transaction host startswith="Transaction started" maxpause=90s&lt;BR /&gt;| search Failure OR Success&lt;BR /&gt;| eval Store= substr(host,1,7)&lt;BR /&gt;| eval Register= substr(host,8,2)&lt;BR /&gt;| rex field=_raw "AdyenPaymentResponse:.+\sResult\s:\s(?&amp;lt;Status&amp;gt;.+)"&lt;BR /&gt;| rex field=_raw "AdyenPaymentResponse:.+\sReason\s:\s(?&amp;lt;Failure_Message&amp;gt;.+)"&lt;BR /&gt;| rex field=_raw "AdyenPaymentResponse:.+\sMessage\s:\s(?&amp;lt;Error_Message&amp;gt;.+)\;"&lt;BR /&gt;| replace "* " with * in Error_Message Failure_Message&lt;BR /&gt;| eval Msg=if((Failure_Message=="200 Emv error " OR Failure_Message=="NoAcquirerFoundConfigured "),"Failed","Declined")&lt;BR /&gt;| stats count(eval(Status="Success")) AS Success_Count count(eval(Status="Failure")) AS Failure_Count sum(eval(Msg="Failed")) AS Failed sum(eval(Msg="Declined")) AS Declined By Store Register&lt;BR /&gt;| eval Total_Payment= Success_Count + Failure_Count&lt;BR /&gt;| table Store Register Success_Count Failure_Count Total_Payment Failed Declined&lt;/P&gt;</description>
      <pubDate>Fri, 14 Jul 2023 11:16:42 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/doing-sum-with-if-condition-in-search-query/m-p/650534#M224903</guid>
      <dc:creator>man03359</dc:creator>
      <dc:date>2023-07-14T11:16:42Z</dc:date>
    </item>
    <item>
      <title>Re: doing sum with if condition in search query</title>
      <link>https://community.splunk.com/t5/Splunk-Search/doing-sum-with-if-condition-in-search-query/m-p/650538#M224905</link>
      <description>&lt;P&gt;When posting code, it is usually best to paste it into a code block &amp;lt;/&amp;gt; that way formatting, such as spaces, is preserved.&lt;/P&gt;&lt;P&gt;Having said that, assuming what you have posted is accurate, you appear to be removing spaces from Failure_Message (and Error_Message) but your if function appears to be assuming they are still there.&lt;/P&gt;&lt;P&gt;Could this be the source of the issue?&lt;/P&gt;</description>
      <pubDate>Fri, 14 Jul 2023 11:43:21 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/doing-sum-with-if-condition-in-search-query/m-p/650538#M224905</guid>
      <dc:creator>ITWhisperer</dc:creator>
      <dc:date>2023-07-14T11:43:21Z</dc:date>
    </item>
    <item>
      <title>Re: doing sum with if condition in search query</title>
      <link>https://community.splunk.com/t5/Splunk-Search/doing-sum-with-if-condition-in-search-query/m-p/650543#M224908</link>
      <description>&lt;P&gt;Yes, because I observed when filtering out that the regex did not extract till the end of the string, since there were only 2 errors I wanted to monitor I manually gave a space there.&amp;nbsp; I want to do the sum of Failed and Declined from this query.&lt;/P&gt;&lt;PRE&gt;&lt;SPAN&gt;&lt;SPAN class=""&gt;&lt;STRONG&gt;| eval Msg=if((Failure_Message=="200 Emv error " OR Failure_Message=="NoAcquirerFoundConfigured "),"Failed","Declined")&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/SPAN&gt; &lt;/PRE&gt;&lt;P&gt;&lt;SPAN&gt;Here's my lame attempt to get it working..&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN&gt;&lt;SPAN class=""&gt;| stats count(eval(Status="Success")) AS Success_Count count(eval(Status="Failure")) AS Failure_Count sum(eval(match(Msg="Failed"))) AS Failed sum(eval(match(Msg="Declined"))) AS Declined By Store Register&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Jul 2023 12:08:11 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/doing-sum-with-if-condition-in-search-query/m-p/650543#M224908</guid>
      <dc:creator>man03359</dc:creator>
      <dc:date>2023-07-14T12:08:11Z</dc:date>
    </item>
    <item>
      <title>Re: doing sum with if condition in search query</title>
      <link>https://community.splunk.com/t5/Splunk-Search/doing-sum-with-if-condition-in-search-query/m-p/650546#M224909</link>
      <description>&lt;P&gt;I suppose you have a problem with&lt;/P&gt;&lt;PRE&gt;| stats sum(eval(condition))&lt;/PRE&gt;&lt;P&gt;Not giving expected results. That's because your expectations are wrong (and the docs, to which I already posted feedback long time ago, are misleading).&lt;/P&gt;&lt;P&gt;People often think about the stats with eval as a form of filtering data for the stats command. And yes, it does effectively work this way for the count aggregation. But it's a neat side effect.&lt;/P&gt;&lt;P&gt;In fact&lt;/P&gt;&lt;PRE&gt;| stats aggregation(eval(expression_A))&lt;/PRE&gt;&lt;P&gt;is equivalent to&lt;/P&gt;&lt;PRE&gt;| eval temporary_field=expression_A | stats aggregation(temporary_field)&lt;/PRE&gt;&lt;P&gt;With the one small quirk that if your expression is a boolean operation it's getting silently cast from true/false to 1/0 so that count with evaled condition does work "as expected".&lt;/P&gt;&lt;P&gt;So if you want to, for example, sum all values higher than a given threshold, you'd want somehting like this:&lt;/P&gt;&lt;PRE&gt;| eval temporary_field=if(myfield&amp;gt;threshold,myfield,0) | stats sum(temporary_field)&lt;/PRE&gt;&lt;P&gt;Which can in turn be a bit shortened to&lt;/P&gt;&lt;PRE&gt;| stats sum(eval(if(myfield&amp;gt;threshold,myfield,0)))&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Jul 2023 12:13:47 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/doing-sum-with-if-condition-in-search-query/m-p/650546#M224909</guid>
      <dc:creator>PickleRick</dc:creator>
      <dc:date>2023-07-14T12:13:47Z</dc:date>
    </item>
    <item>
      <title>Re: doing sum with if condition in search query</title>
      <link>https://community.splunk.com/t5/Splunk-Search/doing-sum-with-if-condition-in-search-query/m-p/650789#M225010</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I tried it like this, as you suggested,&lt;/P&gt;&lt;PRE&gt;| stats sum(eval(if(Failure_Message=="200 Emv error " OR Failure_Message=="NoAcquirerFoundConfigured ","Failed","Declined")))&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But the error I am getting is -&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Error in 'stats' command: You must specify a rename for the aggregation specifier on the dynamically evaluated field 'sum(eval(if(Failure_Message=="200 Emv error " OR Failure_Message=="NoAcquirerFoundConfigured ",Failed,Declined)))'.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Jul 2023 09:43:24 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/doing-sum-with-if-condition-in-search-query/m-p/650789#M225010</guid>
      <dc:creator>man03359</dc:creator>
      <dc:date>2023-07-17T09:43:24Z</dc:date>
    </item>
    <item>
      <title>Re: doing sum with if condition in search query</title>
      <link>https://community.splunk.com/t5/Splunk-Search/doing-sum-with-if-condition-in-search-query/m-p/650790#M225011</link>
      <description>&lt;P&gt;Ahhh, sure. You have to add "AS some_destination_column_name" so that it gets a reasonable name.&lt;/P&gt;</description>
      <pubDate>Mon, 17 Jul 2023 09:53:05 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/doing-sum-with-if-condition-in-search-query/m-p/650790#M225011</guid>
      <dc:creator>PickleRick</dc:creator>
      <dc:date>2023-07-17T09:53:05Z</dc:date>
    </item>
    <item>
      <title>Re: doing sum with if condition in search query</title>
      <link>https://community.splunk.com/t5/Splunk-Search/doing-sum-with-if-condition-in-search-query/m-p/650792#M225012</link>
      <description>&lt;P&gt;I want to sum both the values, Failed as well as Declined.&lt;/P&gt;&lt;P&gt;Earlier I was using&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;| eval Failed=if((Failure_Message=="200 Emv error " OR Failure_Message=="NoAcquirerFoundConfigured "),1,0)&lt;BR /&gt;| eval Declined=if((Failure_Message!="200 Emv error " OR Failure_Message!="NoAcquirerFoundConfigured "),1,0)&lt;/PRE&gt;&lt;P&gt;But when I filter for search Declined=1, if shows 200 Emv error as well. So I modified the query to - All I need is the no of times&amp;nbsp; I got Failed and Declined both&lt;/P&gt;&lt;PRE&gt;| eval Msg=if((Failure_Message="200 Emv error" OR Failure_Message="NoAcquirerFoundConfigured"),"Failed","Declined")&lt;/PRE&gt;</description>
      <pubDate>Mon, 17 Jul 2023 10:02:41 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/doing-sum-with-if-condition-in-search-query/m-p/650792#M225012</guid>
      <dc:creator>man03359</dc:creator>
      <dc:date>2023-07-17T10:02:41Z</dc:date>
    </item>
    <item>
      <title>Re: doing sum with if condition in search query</title>
      <link>https://community.splunk.com/t5/Splunk-Search/doing-sum-with-if-condition-in-search-query/m-p/650802#M225013</link>
      <description>&lt;PRE&gt;| eval Failed=if((Failure_Message=="200 Emv error " OR Failure_Message=="NoAcquirerFoundConfigured "),1,0)&lt;BR /&gt;| eval Declined=if((Failure_Message!="200 Emv error " OR Failure_Message!="NoAcquirerFoundConfigured "),1,0)&lt;/PRE&gt;&lt;P&gt;I don't get this. You will _always_ get your Declined field set to 1 unless there is no Failure_Message set at all. Basics of boolean logic.&lt;/P&gt;</description>
      <pubDate>Mon, 17 Jul 2023 10:43:15 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/doing-sum-with-if-condition-in-search-query/m-p/650802#M225013</guid>
      <dc:creator>PickleRick</dc:creator>
      <dc:date>2023-07-17T10:43:15Z</dc:date>
    </item>
    <item>
      <title>Re: doing sum with if condition in search query</title>
      <link>https://community.splunk.com/t5/Splunk-Search/doing-sum-with-if-condition-in-search-query/m-p/650814#M225018</link>
      <description>&lt;P&gt;How do I proceed. Even this is not giving any output&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;| stats sum(eval(if(Failure_Message=="200 Emv error " OR Failure_Message=="NoAcquirerFoundConfigured ","Failed","Declined"))) AS Failed_Count&lt;/PRE&gt;</description>
      <pubDate>Mon, 17 Jul 2023 12:23:48 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/doing-sum-with-if-condition-in-search-query/m-p/650814#M225018</guid>
      <dc:creator>man03359</dc:creator>
      <dc:date>2023-07-17T12:23:48Z</dc:date>
    </item>
    <item>
      <title>Re: doing sum with if condition in search query</title>
      <link>https://community.splunk.com/t5/Splunk-Search/doing-sum-with-if-condition-in-search-query/m-p/650817#M225019</link>
      <description>&lt;P&gt;OK. What are you trying to do? Look into this command.&lt;/P&gt;&lt;P&gt;You're dynamicaly evaluating a condition and based on that setting the aggregated field to one of two strings. And then you're trying to do a sum, which is a numerical operation, on strings. It doesn't work like that.&lt;/P&gt;&lt;P&gt;If you just want a &lt;STRONG&gt;count&lt;/STRONG&gt; (not sum) of values either fulfilling one condition or the other, you have to simply do two separate aggregations.&lt;/P&gt;&lt;P&gt;Like&lt;/P&gt;&lt;PRE&gt;| stats count(eval(my_first_condition)) as first_aggregation count(eval(my_second_condition)) as second_aggregation&lt;/PRE&gt;</description>
      <pubDate>Mon, 17 Jul 2023 12:32:27 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/doing-sum-with-if-condition-in-search-query/m-p/650817#M225019</guid>
      <dc:creator>PickleRick</dc:creator>
      <dc:date>2023-07-17T12:32:27Z</dc:date>
    </item>
    <item>
      <title>Re: doing sum with if condition in search query</title>
      <link>https://community.splunk.com/t5/Splunk-Search/doing-sum-with-if-condition-in-search-query/m-p/650963#M225065</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;Thanks for the help. I modified my condition part from -&lt;/P&gt;&lt;PRE&gt;| eval Msg=if((Failure_Message="200 Emv error" OR Failure_Message="NoAcquirerFoundConfigured"),"Failed","Declined")&lt;/PRE&gt;&lt;P&gt;to this -&lt;/P&gt;&lt;PRE&gt;| eval Failed=if((Failure_Message=="200 Emv error " OR Failure_Message=="NoAcquirerFoundConfigured "),1,0)&lt;BR /&gt;| eval Declined=if((Failure_Message=="200 Emv error " OR Failure_Message=="NoAcquirerFoundConfigured " OR isnull(Failure_Message)),0,1)&lt;/PRE&gt;&lt;P&gt;and then doing -&lt;/P&gt;&lt;PRE&gt;| stats sum(Failed) AS Failed sum(Declined) AS Declined By Store Register&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;and this worked :0&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jul 2023 10:06:43 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/doing-sum-with-if-condition-in-search-query/m-p/650963#M225065</guid>
      <dc:creator>man03359</dc:creator>
      <dc:date>2023-07-18T10:06:43Z</dc:date>
    </item>
    <item>
      <title>Re: doing sum with if condition in search query</title>
      <link>https://community.splunk.com/t5/Splunk-Search/doing-sum-with-if-condition-in-search-query/m-p/650971#M225066</link>
      <description>&lt;P&gt;That's one way of doing that. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Well done.&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jul 2023 10:36:04 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/doing-sum-with-if-condition-in-search-query/m-p/650971#M225066</guid>
      <dc:creator>PickleRick</dc:creator>
      <dc:date>2023-07-18T10:36:04Z</dc:date>
    </item>
    <item>
      <title>Re: doing sum with if condition in search query</title>
      <link>https://community.splunk.com/t5/Splunk-Search/doing-sum-with-if-condition-in-search-query/m-p/650986#M225075</link>
      <description>&lt;P&gt;Thanks :0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Happy Splunking &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jul 2023 11:56:24 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/doing-sum-with-if-condition-in-search-query/m-p/650986#M225075</guid>
      <dc:creator>man03359</dc:creator>
      <dc:date>2023-07-18T11:56:24Z</dc:date>
    </item>
  </channel>
</rss>

