<?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: eval mean(something) when data is split by another field in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/eval-mean-something-when-data-is-split-by-another-field/m-p/108775#M28339</link>
    <description>&lt;P&gt;You want to use &lt;CODE&gt;eventstats&lt;/CODE&gt;:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=monitoring "Group=errors" | eventstats mean(linecount) as avglinecount | stats sum(linecount) as "Error Count", first(avglinecount) as "Average" by ComputerName
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This works somewhat better than the version with a subsearch, at it only needs a single pass over the data, rather than two passes. Another way that works is:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;   index=monitoring "Group=errors" | sistats mean(linecount),sum(linecount) by ComputerName | eventstats mean(linecount) as avglinecount | stats sum(linecount) as "Error Count" by ComputerName, avglinecount
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This is just a performance tweak on the previous, but it's only useful if you have a very large number of errors per ComputerName, i.e., if the ratio between the size of your original base query (&lt;CODE&gt;index=monitoring "Group=errors"&lt;/CODE&gt;) and the final number of results you get at then end of the full query is very large.&lt;/P&gt;</description>
    <pubDate>Sat, 12 Nov 2011 17:58:52 GMT</pubDate>
    <dc:creator>gkanapathy</dc:creator>
    <dc:date>2011-11-12T17:58:52Z</dc:date>
    <item>
      <title>eval mean(something) when data is split by another field</title>
      <link>https://community.splunk.com/t5/Splunk-Search/eval-mean-something-when-data-is-split-by-another-field/m-p/108770#M28334</link>
      <description>&lt;P&gt;I'm looking to build some reports around error counts in our system.  I've got a splunk search which returns an error count by server (ComputerName) using the following query&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=monitoring "Group=errors" | stats sum(linecount) as "Error Count" by ComputerName
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;What I'd like to do is add another column to this chart whch shows the average/mean/whateveryoucallit of all of the lines (the linecount field), so I would get output something like the following:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;ComputerName     ErrorCount     Average
-------------    -----------    --------
Computer1         2              5
Computer2         10             5
Computer3         3              5  
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I've tried append, and appendcols - neither has worked as I would like.  I think what I really want is an eval statement to define the Average, but I can't seem to get results for both the above and &lt;CODE&gt;eval mean(linecount)&lt;/CODE&gt; because the first search is by ComputerName and the second search is for all items.  Can anyone please point me in the correct direction?&lt;/P&gt;</description>
      <pubDate>Fri, 11 Nov 2011 20:52:39 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/eval-mean-something-when-data-is-split-by-another-field/m-p/108770#M28334</guid>
      <dc:creator>dang</dc:creator>
      <dc:date>2011-11-11T20:52:39Z</dc:date>
    </item>
    <item>
      <title>Re: eval mean(something) when data is split by another field</title>
      <link>https://community.splunk.com/t5/Splunk-Search/eval-mean-something-when-data-is-split-by-another-field/m-p/108771#M28335</link>
      <description>&lt;P&gt;Is there a reason why you don't want to just add the mean as a second statistical operator in the &lt;CODE&gt;stats&lt;/CODE&gt; command?&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=monitoring "Group=errors" | stats sum(linecount) as "Error Count", mean(linecount) as "Average" by ComputerName
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;EDIT: So, I didn't catch originally that you meant an average for all events that should always be included in each stats line. This could be done by making sure that average is always available in a field for each event (I'm calling it "lineavg" in this example) and then pull that absolute value into the &lt;CODE&gt;stats&lt;/CODE&gt; command using something like &lt;CODE&gt;first()&lt;/CODE&gt; or &lt;CODE&gt;max()&lt;/CODE&gt;. To get the average, use a subsearch where the output field is called "query". This will make the subsearch return output that's suitable for &lt;CODE&gt;eval&lt;/CODE&gt;.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=monitoring "Group=errors" | eval lineavg=[search index=monitoring "Group=errors" | stats mean(linecount) as query | fields query] | stats sum(linecount) as "Error Count", first(lineavg) as "Average" by ComputerName
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 11 Nov 2011 21:29:56 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/eval-mean-something-when-data-is-split-by-another-field/m-p/108771#M28335</guid>
      <dc:creator>Ayn</dc:creator>
      <dc:date>2011-11-11T21:29:56Z</dc:date>
    </item>
    <item>
      <title>Re: eval mean(something) when data is split by another field</title>
      <link>https://community.splunk.com/t5/Splunk-Search/eval-mean-something-when-data-is-split-by-another-field/m-p/108772#M28336</link>
      <description>&lt;P&gt;The reason I'm not using that method is that it returns per-computer averages, and that's not what I'm looking for.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Nov 2011 21:37:49 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/eval-mean-something-when-data-is-split-by-another-field/m-p/108772#M28336</guid>
      <dc:creator>dang</dc:creator>
      <dc:date>2011-11-11T21:37:49Z</dc:date>
    </item>
    <item>
      <title>Re: eval mean(something) when data is split by another field</title>
      <link>https://community.splunk.com/t5/Splunk-Search/eval-mean-something-when-data-is-split-by-another-field/m-p/108773#M28337</link>
      <description>&lt;P&gt;Ah, I see. Sorry, didn't catch that. Updating my answer with a suggestion on how to solve your problem.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Nov 2011 22:00:05 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/eval-mean-something-when-data-is-split-by-another-field/m-p/108773#M28337</guid>
      <dc:creator>Ayn</dc:creator>
      <dc:date>2011-11-11T22:00:05Z</dc:date>
    </item>
    <item>
      <title>Re: eval mean(something) when data is split by another field</title>
      <link>https://community.splunk.com/t5/Splunk-Search/eval-mean-something-when-data-is-split-by-another-field/m-p/108774#M28338</link>
      <description>&lt;P&gt;Thanks, Ayn, that seems to do what I want it to do.  Now I just need to wrap my brain around how it all works.  &lt;/P&gt;

&lt;P&gt;For some reason, the math is working out strangely, but that is probably more related to my data than your query, as it all works out logically in small volumes.  Thanks for the help.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Nov 2011 22:25:16 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/eval-mean-something-when-data-is-split-by-another-field/m-p/108774#M28338</guid>
      <dc:creator>dang</dc:creator>
      <dc:date>2011-11-11T22:25:16Z</dc:date>
    </item>
    <item>
      <title>Re: eval mean(something) when data is split by another field</title>
      <link>https://community.splunk.com/t5/Splunk-Search/eval-mean-something-when-data-is-split-by-another-field/m-p/108775#M28339</link>
      <description>&lt;P&gt;You want to use &lt;CODE&gt;eventstats&lt;/CODE&gt;:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=monitoring "Group=errors" | eventstats mean(linecount) as avglinecount | stats sum(linecount) as "Error Count", first(avglinecount) as "Average" by ComputerName
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This works somewhat better than the version with a subsearch, at it only needs a single pass over the data, rather than two passes. Another way that works is:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;   index=monitoring "Group=errors" | sistats mean(linecount),sum(linecount) by ComputerName | eventstats mean(linecount) as avglinecount | stats sum(linecount) as "Error Count" by ComputerName, avglinecount
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This is just a performance tweak on the previous, but it's only useful if you have a very large number of errors per ComputerName, i.e., if the ratio between the size of your original base query (&lt;CODE&gt;index=monitoring "Group=errors"&lt;/CODE&gt;) and the final number of results you get at then end of the full query is very large.&lt;/P&gt;</description>
      <pubDate>Sat, 12 Nov 2011 17:58:52 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/eval-mean-something-when-data-is-split-by-another-field/m-p/108775#M28339</guid>
      <dc:creator>gkanapathy</dc:creator>
      <dc:date>2011-11-12T17:58:52Z</dc:date>
    </item>
    <item>
      <title>Re: eval mean(something) when data is split by another field</title>
      <link>https://community.splunk.com/t5/Splunk-Search/eval-mean-something-when-data-is-split-by-another-field/m-p/108776#M28340</link>
      <description>&lt;P&gt;This is a really good solution. I didn't know about &lt;CODE&gt;eventstats&lt;/CODE&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Apr 2015 21:01:40 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/eval-mean-something-when-data-is-split-by-another-field/m-p/108776#M28340</guid>
      <dc:creator>chustar</dc:creator>
      <dc:date>2015-04-24T21:01:40Z</dc:date>
    </item>
  </channel>
</rss>

