<?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: Difference between stats and chart in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Difference-between-stats-and-chart/m-p/89864#M23036</link>
    <description>&lt;P&gt;Let's compare with two examples: &lt;/P&gt;

&lt;OL&gt;
&lt;LI&gt;&lt;P&gt;&lt;CODE&gt;* | stats sum(x) by user, host, status&lt;/CODE&gt; will output rows that look like: &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;    user       host      status    sum(x) 
    ---------------------------------------
    bob        host1     200       25
    bob        host1     404       12
    bob        host2     404        3
    alice      host1     200       17
    alice      host2     500        1
&lt;/CODE&gt;&lt;/PRE&gt;&lt;/LI&gt;
&lt;/OL&gt;

&lt;P&gt;2) But &lt;CODE&gt;* | chart sum(x) over user by status&lt;/CODE&gt; will output quite different rows that look like. &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;        user       200       404      500
        ---------------------------------------
        bob         25        15         
        alice       17                  1
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Note that the first example incorporates data about the "host" field, whereas the second one does not.  We'll come back to this. &lt;/P&gt;

&lt;P&gt;In more formal terms,   &lt;CODE&gt;stats sum(x) by user, host, status&lt;/CODE&gt; will create one row for each combination of user, host and status that are present in the data.   Then for each of those rows it will also compute whatever statistic(s) or function(s) you tell it (here it's just &lt;CODE&gt;sum(x)&lt;/CODE&gt;). &lt;/P&gt;

&lt;P&gt;On the other hand, the chart command,  will create rows that are each of the values of the single "group by" field,  and COLUMNS that are each of the values of the "split by" field.    (btw the &lt;CODE&gt;timechart&lt;/CODE&gt; command you can sort of think of chart that is locked into using _time as the "group-by" field, although the reality is a little more complex)&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;Some Interesting Upshots&lt;/STRONG&gt;&lt;/P&gt;

&lt;OL&gt;
&lt;LI&gt;&lt;P&gt;Note that you can specify any number of "group by" fields to the stats command, whereas the chart/timechart command can only have one "group by" (with timechart it is always _time)  and one "split by".  This is why our first example was able to incorporate the "host" field easily whereas the second example did not. &lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;P&gt;This creates a concept of a "stats style" result set,  versus a "chart style" result set.   I say "style" because I mean it looks like the output of the given command, even if it didn't necessarily come from that command.   ie &lt;CODE&gt;|inputlookup foo&lt;/CODE&gt;  might well emerge blinking into the light of your browser and be a "chart style" set.      This has some implications that you get used to,  like "filling in last known values" in a stats-style set is generally done with the &lt;CODE&gt;streamstats&lt;/CODE&gt; command, whereas doing the thing with chart-style results is more often done with the &lt;CODE&gt;filldown&lt;/CODE&gt; command. &lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;P&gt;The stats command will throw away any events where one or more of the "group" by fields does not exist.  If you want it to keep them, you have to use an explicit &lt;A href="http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Fillnull"&gt;fillnull command.&lt;/A&gt;    The chart/timechart commands will likewise throw away events where the single "group by" field doesn't exist,  but it will actually roll up all the null values of the "split by" field into a big column called "NULL" which you can fiddle with and/or suppress &lt;A href="http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Chart"&gt;with various arguments&lt;/A&gt;.   &lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;P&gt;You can always transform your results from a "stats style" result set to the "chart style" with &lt;A href="http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Xyseries"&gt;the xyseries command&lt;/A&gt;.  eg  &lt;CODE&gt;xyseries foo bar baz&lt;/CODE&gt;, or if you will &lt;CODE&gt;xyseries  groupByField splitByField computedStatistic&lt;/CODE&gt;. &lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;P&gt;Going the other way,  you can transform your results from a "chart style" result set to the "stats style" with &lt;A href="http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Untable"&gt;the untable command&lt;/A&gt;. eg  &lt;CODE&gt;| untable foo bar baz&lt;/CODE&gt;,  or labeling the fields,   &lt;CODE&gt;| untable  groupByField splitByField computedStatistic&lt;/CODE&gt;. &lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;P&gt;Following from this,   &lt;CODE&gt;| xyseries foo bar baz | untable  foo bar baz&lt;/CODE&gt; negates itself and so is a fun way to do nothing at all. &lt;span class="lia-unicode-emoji" title=":grinning_face_with_big_eyes:"&gt;😃&lt;/span&gt;&lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;P&gt;As you might guess from the runaway bullet points here, this is a deep topic.  Not uncommonly a single search might start out doing things in one style, because it needs to use eval in a certain way, and then switch it all over to the other style because it needs to do some other thing that needs "chart-style" rows. &lt;/P&gt;&lt;/LI&gt;
&lt;/OL&gt;

&lt;P&gt;&lt;STRONG&gt;Other things that are a little confusing.&lt;/STRONG&gt; &lt;/P&gt;

&lt;P&gt;-- You can also use chart command with no split-by field specified at all, and in such cases it behaves identically to the stats command.    eg   &lt;CODE&gt;stats count by foo&lt;/CODE&gt;  is exactly the same as &lt;CODE&gt;chart count over foo&lt;/CODE&gt;.    So some people think of "chart" as being an alias to "stats" when actually it's quite important and does things nothing else can. &lt;/P&gt;

&lt;P&gt;-- The chart command also allows you to express it as &lt;CODE&gt;chart count by foo, bar&lt;/CODE&gt;   which looks a lot like the stats syntax.  HOWEVER, chart recognizes the first field foo as the "group by" field, thus becoming the output rows,   and the second field is recognized as the "split by" field, becoming the column names across the top. To avoid this confusion I recommend avoiding the &lt;CODE&gt;chart count by foo bar&lt;/CODE&gt; syntax entirely, and instead try and do &lt;CODE&gt;chart count over foo by bar&lt;/CODE&gt;.  It's a bit more verbose but it will help new users avoid this confusion. (random trivia: it was actually me that lobbied for the "over" syntax as a result of which it got snuck into a 4.X release)&lt;/P&gt;</description>
    <pubDate>Tue, 29 Sep 2015 19:17:24 GMT</pubDate>
    <dc:creator>sideview</dc:creator>
    <dc:date>2015-09-29T19:17:24Z</dc:date>
    <item>
      <title>Difference between stats and chart</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Difference-between-stats-and-chart/m-p/89858#M23030</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;

&lt;P&gt;I have been working with Splunk for quite a while now. Still I am wondering:&lt;/P&gt;

&lt;P&gt;Whatis the difference between chart and stats? Most of the time I find myself getting the same results no matter which function I am using...&lt;/P&gt;

&lt;P&gt;Kind regards,&lt;BR /&gt;
Katsche&lt;/P&gt;</description>
      <pubDate>Tue, 11 Oct 2011 13:01:09 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Difference-between-stats-and-chart/m-p/89858#M23030</guid>
      <dc:creator>Katsche</dc:creator>
      <dc:date>2011-10-11T13:01:09Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between stats and chart</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Difference-between-stats-and-chart/m-p/89859#M23031</link>
      <description>&lt;P&gt;Good question &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;  Normally, I use &lt;CODE&gt;chart&lt;/CODE&gt; if I'm looking for a visual display, and the &lt;CODE&gt;stats&lt;/CODE&gt; search command for anything else.  But they do seem fairly interchangeably (certainly all the stats-related command share common functions, which is nice.)  Perhaps there's a legacy reason for having both.  I'm looking forward to an informed answer.&lt;/P&gt;</description>
      <pubDate>Tue, 11 Oct 2011 14:28:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Difference-between-stats-and-chart/m-p/89859#M23031</guid>
      <dc:creator>Lowell</dc:creator>
      <dc:date>2011-10-11T14:28:34Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between stats and chart</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Difference-between-stats-and-chart/m-p/89860#M23032</link>
      <description>&lt;P&gt;I don't claim to know the full truth here either, but you can see how they commands differ when generating statistics split by two fields. &lt;CODE&gt;stats&lt;/CODE&gt; will stack the values of field2 after each other whereas &lt;CODE&gt;chart&lt;/CODE&gt; will generate a matrix with one column for each value of field2.&lt;/P&gt;

&lt;P&gt;Have a look at this search for example:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=_internal | chart count by host,sourcetype
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;vs&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=_internal | stats count by host,sourcetype
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 11 Oct 2011 14:40:40 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Difference-between-stats-and-chart/m-p/89860#M23032</guid>
      <dc:creator>Ayn</dc:creator>
      <dc:date>2011-10-11T14:40:40Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between stats and chart</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Difference-between-stats-and-chart/m-p/89861#M23033</link>
      <description>&lt;P&gt;can i sort the count field with the chart command..tried like this but didnt worked&lt;/P&gt;

&lt;P&gt;index=_internal | stats count by host,sourcetype | sort -count | chart count by host,sourcetyp&lt;/P&gt;</description>
      <pubDate>Wed, 06 Jun 2012 10:02:15 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Difference-between-stats-and-chart/m-p/89861#M23033</guid>
      <dc:creator>rakesh_498115</dc:creator>
      <dc:date>2012-06-06T10:02:15Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between stats and chart</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Difference-between-stats-and-chart/m-p/89862#M23034</link>
      <description>&lt;P&gt;I think that the chart ... by ... rearranges to sort the results by the by-field(s) so-to-speak. /k&lt;/P&gt;</description>
      <pubDate>Wed, 06 Jun 2012 10:25:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Difference-between-stats-and-chart/m-p/89862#M23034</guid>
      <dc:creator>kristian_kolb</dc:creator>
      <dc:date>2012-06-06T10:25:34Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between stats and chart</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Difference-between-stats-and-chart/m-p/89863#M23035</link>
      <description>&lt;P&gt;When using split-by clause in chart command, the output would be a table with distinct values of the split-by field. Whereas in stats command, all of the split-by field would be included (even duplicate ones). For e.g.&lt;/P&gt;

&lt;P&gt;index=_internal | stats count by  date_hour,sourcetype&lt;/P&gt;

&lt;P&gt;index=_internal | chart count over sourcetype by date_hour&lt;/P&gt;

&lt;P&gt;Will give you different output because of "by" field. Not because of over &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 06:56:52 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Difference-between-stats-and-chart/m-p/89863#M23035</guid>
      <dc:creator>meenal901</dc:creator>
      <dc:date>2020-09-29T06:56:52Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between stats and chart</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Difference-between-stats-and-chart/m-p/89864#M23036</link>
      <description>&lt;P&gt;Let's compare with two examples: &lt;/P&gt;

&lt;OL&gt;
&lt;LI&gt;&lt;P&gt;&lt;CODE&gt;* | stats sum(x) by user, host, status&lt;/CODE&gt; will output rows that look like: &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;    user       host      status    sum(x) 
    ---------------------------------------
    bob        host1     200       25
    bob        host1     404       12
    bob        host2     404        3
    alice      host1     200       17
    alice      host2     500        1
&lt;/CODE&gt;&lt;/PRE&gt;&lt;/LI&gt;
&lt;/OL&gt;

&lt;P&gt;2) But &lt;CODE&gt;* | chart sum(x) over user by status&lt;/CODE&gt; will output quite different rows that look like. &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;        user       200       404      500
        ---------------------------------------
        bob         25        15         
        alice       17                  1
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Note that the first example incorporates data about the "host" field, whereas the second one does not.  We'll come back to this. &lt;/P&gt;

&lt;P&gt;In more formal terms,   &lt;CODE&gt;stats sum(x) by user, host, status&lt;/CODE&gt; will create one row for each combination of user, host and status that are present in the data.   Then for each of those rows it will also compute whatever statistic(s) or function(s) you tell it (here it's just &lt;CODE&gt;sum(x)&lt;/CODE&gt;). &lt;/P&gt;

&lt;P&gt;On the other hand, the chart command,  will create rows that are each of the values of the single "group by" field,  and COLUMNS that are each of the values of the "split by" field.    (btw the &lt;CODE&gt;timechart&lt;/CODE&gt; command you can sort of think of chart that is locked into using _time as the "group-by" field, although the reality is a little more complex)&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;Some Interesting Upshots&lt;/STRONG&gt;&lt;/P&gt;

&lt;OL&gt;
&lt;LI&gt;&lt;P&gt;Note that you can specify any number of "group by" fields to the stats command, whereas the chart/timechart command can only have one "group by" (with timechart it is always _time)  and one "split by".  This is why our first example was able to incorporate the "host" field easily whereas the second example did not. &lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;P&gt;This creates a concept of a "stats style" result set,  versus a "chart style" result set.   I say "style" because I mean it looks like the output of the given command, even if it didn't necessarily come from that command.   ie &lt;CODE&gt;|inputlookup foo&lt;/CODE&gt;  might well emerge blinking into the light of your browser and be a "chart style" set.      This has some implications that you get used to,  like "filling in last known values" in a stats-style set is generally done with the &lt;CODE&gt;streamstats&lt;/CODE&gt; command, whereas doing the thing with chart-style results is more often done with the &lt;CODE&gt;filldown&lt;/CODE&gt; command. &lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;P&gt;The stats command will throw away any events where one or more of the "group" by fields does not exist.  If you want it to keep them, you have to use an explicit &lt;A href="http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Fillnull"&gt;fillnull command.&lt;/A&gt;    The chart/timechart commands will likewise throw away events where the single "group by" field doesn't exist,  but it will actually roll up all the null values of the "split by" field into a big column called "NULL" which you can fiddle with and/or suppress &lt;A href="http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Chart"&gt;with various arguments&lt;/A&gt;.   &lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;P&gt;You can always transform your results from a "stats style" result set to the "chart style" with &lt;A href="http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Xyseries"&gt;the xyseries command&lt;/A&gt;.  eg  &lt;CODE&gt;xyseries foo bar baz&lt;/CODE&gt;, or if you will &lt;CODE&gt;xyseries  groupByField splitByField computedStatistic&lt;/CODE&gt;. &lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;P&gt;Going the other way,  you can transform your results from a "chart style" result set to the "stats style" with &lt;A href="http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Untable"&gt;the untable command&lt;/A&gt;. eg  &lt;CODE&gt;| untable foo bar baz&lt;/CODE&gt;,  or labeling the fields,   &lt;CODE&gt;| untable  groupByField splitByField computedStatistic&lt;/CODE&gt;. &lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;P&gt;Following from this,   &lt;CODE&gt;| xyseries foo bar baz | untable  foo bar baz&lt;/CODE&gt; negates itself and so is a fun way to do nothing at all. &lt;span class="lia-unicode-emoji" title=":grinning_face_with_big_eyes:"&gt;😃&lt;/span&gt;&lt;/P&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;P&gt;As you might guess from the runaway bullet points here, this is a deep topic.  Not uncommonly a single search might start out doing things in one style, because it needs to use eval in a certain way, and then switch it all over to the other style because it needs to do some other thing that needs "chart-style" rows. &lt;/P&gt;&lt;/LI&gt;
&lt;/OL&gt;

&lt;P&gt;&lt;STRONG&gt;Other things that are a little confusing.&lt;/STRONG&gt; &lt;/P&gt;

&lt;P&gt;-- You can also use chart command with no split-by field specified at all, and in such cases it behaves identically to the stats command.    eg   &lt;CODE&gt;stats count by foo&lt;/CODE&gt;  is exactly the same as &lt;CODE&gt;chart count over foo&lt;/CODE&gt;.    So some people think of "chart" as being an alias to "stats" when actually it's quite important and does things nothing else can. &lt;/P&gt;

&lt;P&gt;-- The chart command also allows you to express it as &lt;CODE&gt;chart count by foo, bar&lt;/CODE&gt;   which looks a lot like the stats syntax.  HOWEVER, chart recognizes the first field foo as the "group by" field, thus becoming the output rows,   and the second field is recognized as the "split by" field, becoming the column names across the top. To avoid this confusion I recommend avoiding the &lt;CODE&gt;chart count by foo bar&lt;/CODE&gt; syntax entirely, and instead try and do &lt;CODE&gt;chart count over foo by bar&lt;/CODE&gt;.  It's a bit more verbose but it will help new users avoid this confusion. (random trivia: it was actually me that lobbied for the "over" syntax as a result of which it got snuck into a 4.X release)&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2015 19:17:24 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Difference-between-stats-and-chart/m-p/89864#M23036</guid>
      <dc:creator>sideview</dc:creator>
      <dc:date>2015-09-29T19:17:24Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between stats and chart</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Difference-between-stats-and-chart/m-p/89865#M23037</link>
      <description>&lt;P&gt;This should be the accepted answer, and is worth reading.&lt;/P&gt;</description>
      <pubDate>Thu, 13 Oct 2016 14:15:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Difference-between-stats-and-chart/m-p/89865#M23037</guid>
      <dc:creator>bhawkins1</dc:creator>
      <dc:date>2016-10-13T14:15:13Z</dc:date>
    </item>
    <item>
      <title>Re: Difference between stats and chart</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Difference-between-stats-and-chart/m-p/89866#M23038</link>
      <description>&lt;P&gt;Is there any performance related issues that may arise while using chart function over stats function? I mean which one takes a longer time to execute in a complex search query (with multiple appends, yet searched at index level).&lt;/P&gt;</description>
      <pubDate>Fri, 17 Feb 2017 10:21:29 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Difference-between-stats-and-chart/m-p/89866#M23038</guid>
      <dc:creator>sundarrajan</dc:creator>
      <dc:date>2017-02-17T10:21:29Z</dc:date>
    </item>
  </channel>
</rss>

