<?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: Omitting zero values when calculating stats avg()? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Omitting-zero-values-when-calculating-stats-avg/m-p/102075#M26369</link>
    <description>&lt;P&gt;Since avg excludes nulls, you can use eval to turn 0 into null, like:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | stats avg(eval(if(MyValue==0, null(), MyValue))) as Avg
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Here it's working in a toy example:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| stats count | append [stats count | stats count] | stats avg(eval(if(count==0, null(), count))) as avg
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 27 Mar 2012 15:41:37 GMT</pubDate>
    <dc:creator>Stephen_Sorkin</dc:creator>
    <dc:date>2012-03-27T15:41:37Z</dc:date>
    <item>
      <title>Omitting zero values when calculating stats avg()?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Omitting-zero-values-when-calculating-stats-avg/m-p/102072#M26366</link>
      <description>&lt;P&gt;What I want is: &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | stats avg(eval(MyValue!=0)) as Avg
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;It doesn't work that way (Avg is always 1.0).&lt;/P&gt;

&lt;P&gt;Of course, the workaround is:  &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | stats sum(MyValue) as Sum, count(eval(MyValue!=0)) as Count | eval Avg = Sum/Count
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;But is there a simpler way of using a conditional expression with stats avg()?&lt;/P&gt;</description>
      <pubDate>Tue, 27 Mar 2012 14:20:01 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Omitting-zero-values-when-calculating-stats-avg/m-p/102072#M26366</guid>
      <dc:creator>greg</dc:creator>
      <dc:date>2012-03-27T14:20:01Z</dc:date>
    </item>
    <item>
      <title>Re: Omitting zero values when calculating stats avg()?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Omitting-zero-values-when-calculating-stats-avg/m-p/102073#M26367</link>
      <description>&lt;P&gt;How about&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;search ... AND NOT MyValue=0
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;?&lt;/P&gt;</description>
      <pubDate>Tue, 27 Mar 2012 14:26:12 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Omitting-zero-values-when-calculating-stats-avg/m-p/102073#M26367</guid>
      <dc:creator>Ayn</dc:creator>
      <dc:date>2012-03-27T14:26:12Z</dc:date>
    </item>
    <item>
      <title>Re: Omitting zero values when calculating stats avg()?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Omitting-zero-values-when-calculating-stats-avg/m-p/102074#M26368</link>
      <description>&lt;P&gt;Is your idea in preliminary filtering of zero values before stats? &lt;BR /&gt;
In my case, "search ..." is a complicated search about 10 lines long and I can't just put AND NOT at the end. Using ... | where MyValue!=0 | stats ... filters out some necessary fields together with MyValue, so it's not the case also.&lt;/P&gt;</description>
      <pubDate>Tue, 27 Mar 2012 14:43:14 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Omitting-zero-values-when-calculating-stats-avg/m-p/102074#M26368</guid>
      <dc:creator>greg</dc:creator>
      <dc:date>2012-03-27T14:43:14Z</dc:date>
    </item>
    <item>
      <title>Re: Omitting zero values when calculating stats avg()?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Omitting-zero-values-when-calculating-stats-avg/m-p/102075#M26369</link>
      <description>&lt;P&gt;Since avg excludes nulls, you can use eval to turn 0 into null, like:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | stats avg(eval(if(MyValue==0, null(), MyValue))) as Avg
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Here it's working in a toy example:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| stats count | append [stats count | stats count] | stats avg(eval(if(count==0, null(), count))) as avg
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 27 Mar 2012 15:41:37 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Omitting-zero-values-when-calculating-stats-avg/m-p/102075#M26369</guid>
      <dc:creator>Stephen_Sorkin</dc:creator>
      <dc:date>2012-03-27T15:41:37Z</dc:date>
    </item>
    <item>
      <title>Re: Omitting zero values when calculating stats avg()?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Omitting-zero-values-when-calculating-stats-avg/m-p/102076#M26370</link>
      <description>&lt;P&gt;Thanks Stephen, this trick works great!&lt;BR /&gt;
Is it mentioned somewhere in docs (about avg() and nulls), could you point out?&lt;/P&gt;</description>
      <pubDate>Tue, 27 Mar 2012 15:57:54 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Omitting-zero-values-when-calculating-stats-avg/m-p/102076#M26370</guid>
      <dc:creator>greg</dc:creator>
      <dc:date>2012-03-27T15:57:54Z</dc:date>
    </item>
    <item>
      <title>Re: Omitting zero values when calculating stats avg()?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Omitting-zero-values-when-calculating-stats-avg/m-p/102077#M26371</link>
      <description>&lt;P&gt;The docs for stats aggregators is at &lt;A href="http://docs.splunk.com/Documentation/Splunk/4.3.1/SearchReference/CommonStatsFunctions"&gt;http://docs.splunk.com/Documentation/Splunk/4.3.1/SearchReference/CommonStatsFunctions&lt;/A&gt;. However, it doesn't explicitly mention this, since it's just a consequence of the definition of avg as sum divided by count, and count is the number of occurrences.&lt;/P&gt;</description>
      <pubDate>Tue, 27 Mar 2012 16:16:10 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Omitting-zero-values-when-calculating-stats-avg/m-p/102077#M26371</guid>
      <dc:creator>Stephen_Sorkin</dc:creator>
      <dc:date>2012-03-27T16:16:10Z</dc:date>
    </item>
    <item>
      <title>Re: Omitting zero values when calculating stats avg()?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Omitting-zero-values-when-calculating-stats-avg/m-p/102078#M26372</link>
      <description>&lt;P&gt;what about before doing the stats command using table/fields to list out all the fields you want to perform stats and then doing this command &lt;/P&gt;

&lt;P&gt;| replace 0 with "" in MyValue&lt;/P&gt;

&lt;P&gt;so it would be &lt;/P&gt;

&lt;P&gt;.....| fields col1 col2 col3 MyValue | replace "0" with "" in MyValue | stats avg(MyValue)&lt;/P&gt;

&lt;P&gt;You can also use &lt;BR /&gt;
| replace "0" with ""&lt;BR /&gt;
to replace "0" in all fields&lt;/P&gt;</description>
      <pubDate>Fri, 02 Aug 2013 19:26:25 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Omitting-zero-values-when-calculating-stats-avg/m-p/102078#M26372</guid>
      <dc:creator>cramasta</dc:creator>
      <dc:date>2013-08-02T19:26:25Z</dc:date>
    </item>
    <item>
      <title>Re: Omitting zero values when calculating stats avg()?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Omitting-zero-values-when-calculating-stats-avg/m-p/102079#M26373</link>
      <description>&lt;P&gt;Well, what's the advantage of your method in comparison with Stephen's answer? I mean, between replacing 0 with an empty string ("") before stats and doing eval(if(...)) which turns 0 to null inside stats? &lt;BR /&gt;
If stats treats empty strings as nulls, then it works pretty the same way.&lt;/P&gt;</description>
      <pubDate>Mon, 05 Aug 2013 10:01:18 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Omitting-zero-values-when-calculating-stats-avg/m-p/102079#M26373</guid>
      <dc:creator>greg</dc:creator>
      <dc:date>2013-08-05T10:01:18Z</dc:date>
    </item>
    <item>
      <title>Re: Omitting zero values when calculating stats avg()?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Omitting-zero-values-when-calculating-stats-avg/m-p/102080#M26374</link>
      <description>&lt;P&gt;Nothing wrong with Steven's answer, its probably the best way to do most of the time. The option I provided comes in handy if you have 20 fields in a single event that you wanted to get an average for. You would have to create 20 eval's for each field using Steven's method. Using one replace command takes care of all the fields all at once.   Also it can come in handy if the 20 fields are constantly changing and you cant predict ahead of time what all the field names are going to be.&lt;/P&gt;</description>
      <pubDate>Mon, 05 Aug 2013 14:05:53 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Omitting-zero-values-when-calculating-stats-avg/m-p/102080#M26374</guid>
      <dc:creator>cramasta</dc:creator>
      <dc:date>2013-08-05T14:05:53Z</dc:date>
    </item>
    <item>
      <title>Re: Omitting zero values when calculating stats avg()?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Omitting-zero-values-when-calculating-stats-avg/m-p/102081#M26375</link>
      <description>&lt;P&gt;Ah, I see. Then it's a good thing to know about &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 05 Aug 2013 14:09:32 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Omitting-zero-values-when-calculating-stats-avg/m-p/102081#M26375</guid>
      <dc:creator>greg</dc:creator>
      <dc:date>2013-08-05T14:09:32Z</dc:date>
    </item>
  </channel>
</rss>

