<?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 Re: stats conditional count in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/stats-conditional-count/m-p/132298#M36080</link>
    <description>&lt;P&gt;.. adding by field4, of course.  These have the exact same effect?  Is either method better or faster?  Is there a better way than those two?&lt;/P&gt;</description>
    <pubDate>Tue, 15 Apr 2014 15:46:54 GMT</pubDate>
    <dc:creator>landen99</dc:creator>
    <dc:date>2014-04-15T15:46:54Z</dc:date>
    <item>
      <title>stats conditional count</title>
      <link>https://community.splunk.com/t5/Splunk-Search/stats-conditional-count/m-p/132296#M36078</link>
      <description>&lt;P&gt;I want to count the number of times that the following event is true, bool = ((field1 &amp;lt;&amp;gt; field2) AND (field3 &amp;lt; 8)), for each event by field4.  The two methods in consideration are: 1) eval if and stats sum, and 2) stats if count.&lt;/P&gt;

&lt;P&gt;How can I make these methods work, if possible? I want to understand the functions in this context.  Also, is there a better way?&lt;/P&gt;

&lt;P&gt;Here is my eval approach, so far:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval bool = ((field1 &amp;lt;&amp;gt; field2) AND (field3 &amp;lt; 8)) | eval field_bool = if (bool, 1, 0) | stats sum(field_bool) by field4
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Here is my stats approach, so far:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval bool = ((field1 &amp;lt;&amp;gt; field2) AND (field3 &amp;lt; 8)) | stats if(bool, count) by field4
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 15 Apr 2014 15:05:46 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/stats-conditional-count/m-p/132296#M36078</guid>
      <dc:creator>landen99</dc:creator>
      <dc:date>2014-04-15T15:05:46Z</dc:date>
    </item>
    <item>
      <title>Re: stats conditional count</title>
      <link>https://community.splunk.com/t5/Splunk-Search/stats-conditional-count/m-p/132297#M36079</link>
      <description>&lt;P&gt;You can do one of two things:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;base search | eval bool = if((field1 != field2) AND (field3 &amp;lt; 8), 1, 0) | stats sum(bool) as count
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;or&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;base search | stats count(eval((field1 != field2) AND (field3 &amp;lt; 8))) as count
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 15 Apr 2014 15:38:28 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/stats-conditional-count/m-p/132297#M36079</guid>
      <dc:creator>martin_mueller</dc:creator>
      <dc:date>2014-04-15T15:38:28Z</dc:date>
    </item>
    <item>
      <title>Re: stats conditional count</title>
      <link>https://community.splunk.com/t5/Splunk-Search/stats-conditional-count/m-p/132298#M36080</link>
      <description>&lt;P&gt;.. adding by field4, of course.  These have the exact same effect?  Is either method better or faster?  Is there a better way than those two?&lt;/P&gt;</description>
      <pubDate>Tue, 15 Apr 2014 15:46:54 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/stats-conditional-count/m-p/132298#M36080</guid>
      <dc:creator>landen99</dc:creator>
      <dc:date>2014-04-15T15:46:54Z</dc:date>
    </item>
    <item>
      <title>Re: stats conditional count</title>
      <link>https://community.splunk.com/t5/Splunk-Search/stats-conditional-count/m-p/132299#M36081</link>
      <description>&lt;P&gt;Speed should be very similar. I prefer the first because it separates computing the condition from building the report. If you have multiple such conditions the &lt;CODE&gt;stats&lt;/CODE&gt; in way 2 would become insanely long and impossible to maintain.&lt;/P&gt;

&lt;P&gt;I don't see a better way, because this is as short as it gets. Compute condition, sum up cases where it matched. No step to leave out in there to still achieve the goal.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Apr 2014 15:49:48 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/stats-conditional-count/m-p/132299#M36081</guid>
      <dc:creator>martin_mueller</dc:creator>
      <dc:date>2014-04-15T15:49:48Z</dc:date>
    </item>
    <item>
      <title>Re: stats conditional count</title>
      <link>https://community.splunk.com/t5/Splunk-Search/stats-conditional-count/m-p/132300#M36082</link>
      <description>&lt;P&gt;Would it work just as well or better to remove the "if" function for the boolean evaluation for the first method like this?:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;base search | eval bool = (field1 != field2) AND (field3 &amp;lt; 8) | stats sum(bool) as count
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Added:  It is giving me the error: "Error in 'eval' command: Fields cannot be assigned a boolean result. Instead, try if([bool expr], [expr], [expr])."  So, no, the boolean expression is not treated as 1 for true and 0 for false.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Apr 2014 16:39:43 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/stats-conditional-count/m-p/132300#M36082</guid>
      <dc:creator>landen99</dc:creator>
      <dc:date>2014-04-15T16:39:43Z</dc:date>
    </item>
    <item>
      <title>Re: stats conditional count</title>
      <link>https://community.splunk.com/t5/Splunk-Search/stats-conditional-count/m-p/132301#M36083</link>
      <description>&lt;P&gt;Yeah, you cannot have a field that contains a boolean value.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Apr 2014 17:08:29 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/stats-conditional-count/m-p/132301#M36083</guid>
      <dc:creator>martin_mueller</dc:creator>
      <dc:date>2014-04-15T17:08:29Z</dc:date>
    </item>
    <item>
      <title>Re: stats conditional count</title>
      <link>https://community.splunk.com/t5/Splunk-Search/stats-conditional-count/m-p/132302#M36084</link>
      <description>&lt;P&gt;The following search filter all http status 2xx, 4xx and 5xx and create a field to with the percentage of http status 200 comparing with errors 400 and 500. If status 200 is lower than 94%, an "Warning" is applied.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;base search | rename message.status as msg_status, message.fwdHost as hhost | search msg_status=2* OR msg_status=4* OR msg_status=5* | rangemap field=msg_status "200 Sucesso"=200-299 default="400-599 Erros" | eval ok=if((range = "200 Sucesso"), 1, 0) | eval nok=if((range = "400-599 Erros"), 1, 0) | stats sum(ok) as ok sum(nok) as nok by hhost | addtotals | eval p_ok=ok/Total*100 | rangemap field=p_ok "Normal"=94-100 default="Warning"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;STRONG&gt;The result was like this:&lt;/STRONG&gt;&lt;BR /&gt;
hhost;ok;nok;p_ok;range;Total&lt;BR /&gt;
cgws.domain.com;2055;102;95.271210;Normal;2157&lt;BR /&gt;
dn.domain.com;6;1;85.714286;Warning;7&lt;BR /&gt;
ecommerce.domain.com;106115;646;99.394910;Normal;106761&lt;/P&gt;</description>
      <pubDate>Tue, 22 Nov 2016 15:09:19 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/stats-conditional-count/m-p/132302#M36084</guid>
      <dc:creator>moisesroth</dc:creator>
      <dc:date>2016-11-22T15:09:19Z</dc:date>
    </item>
  </channel>
</rss>

