<?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 stats value count by in two different result in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/stats-value-count-by-in-two-different-result/m-p/433126#M168360</link>
    <description>&lt;P&gt;I am using two spl which the result are different, but I think the spl is the same. Can anyone help?&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=main sourcetype=access_combined_wcookie
|stats values(useragent) as "Agents Used" count as "Times Used" by useragent
|table "Agents Used", "Times Used"

index=main sourcetype=access_combined_wcookie
|stats values(useragent) as "Agents Used" 
|stats count("Agents Used") as "Times Used" by "Agents Used"
|table "Agents Used", "Times Used"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The result is attached. &lt;BR /&gt;
&lt;span class="lia-inline-image-display-wrapper" image-alt="![alt text][1]"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/5353iEDFA22AD2DE403B8/image-size/large?v=v2&amp;amp;px=999" role="button" title="![alt text][1]" alt="![alt text][1]" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Thu, 12 Jul 2018 03:12:41 GMT</pubDate>
    <dc:creator>elbywong</dc:creator>
    <dc:date>2018-07-12T03:12:41Z</dc:date>
    <item>
      <title>stats value count by in two different result</title>
      <link>https://community.splunk.com/t5/Splunk-Search/stats-value-count-by-in-two-different-result/m-p/433126#M168360</link>
      <description>&lt;P&gt;I am using two spl which the result are different, but I think the spl is the same. Can anyone help?&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=main sourcetype=access_combined_wcookie
|stats values(useragent) as "Agents Used" count as "Times Used" by useragent
|table "Agents Used", "Times Used"

index=main sourcetype=access_combined_wcookie
|stats values(useragent) as "Agents Used" 
|stats count("Agents Used") as "Times Used" by "Agents Used"
|table "Agents Used", "Times Used"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The result is attached. &lt;BR /&gt;
&lt;span class="lia-inline-image-display-wrapper" image-alt="![alt text][1]"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/5353iEDFA22AD2DE403B8/image-size/large?v=v2&amp;amp;px=999" role="button" title="![alt text][1]" alt="![alt text][1]" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jul 2018 03:12:41 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/stats-value-count-by-in-two-different-result/m-p/433126#M168360</guid>
      <dc:creator>elbywong</dc:creator>
      <dc:date>2018-07-12T03:12:41Z</dc:date>
    </item>
    <item>
      <title>Re: stats value count by in two different result</title>
      <link>https://community.splunk.com/t5/Splunk-Search/stats-value-count-by-in-two-different-result/m-p/433127#M168361</link>
      <description>&lt;P&gt;@elbywong&lt;/P&gt;

&lt;P&gt;In your first search you have used &lt;CODE&gt;stats&lt;/CODE&gt; command to count events &lt;CODE&gt;useragent&lt;/CODE&gt; and to get &lt;CODE&gt;useragent&lt;/CODE&gt; as &lt;STRONG&gt;Agents Used&lt;/STRONG&gt;.  This will gives &lt;CODE&gt;useragent&lt;/CODE&gt; wise total event count.  Here we will get the proper count of events.&lt;/P&gt;

&lt;P&gt;As per my suggestion, if you remove the &lt;CODE&gt;values(useragent) as "Agents Used"&lt;/CODE&gt; and &lt;CODE&gt;rename&lt;/CODE&gt; the &lt;CODE&gt;useragent&lt;/CODE&gt; field then it will give the same result with proper approach.&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;like&lt;/STRONG&gt;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=main sourcetype=access_combined_wcookie
|stats count as "Times Used" by useragent 
| rename useragent as "Agents Used"
|table "Agents Used", "Times Used"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;In your second search, you have written &lt;CODE&gt;values(useragent) as "Agents Used"&lt;/CODE&gt;, if you execute below search then it will give you &lt;CODE&gt;multivalued&lt;/CODE&gt; &lt;STRONG&gt;Agents Used&lt;/STRONG&gt; field. &lt;/P&gt;

&lt;P&gt;Try:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=main sourcetype=access_combined_wcookie
|stats values(useragent) as "Agents Used" 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Here we are losing total events for particular Agents and gives you a single field with multiple agents. And, as we have used &lt;CODE&gt;stats&lt;/CODE&gt; &lt;BR /&gt;
 command,  we will not able to take the events or count of events which can be used in next &lt;CODE&gt;stats&lt;/CODE&gt; command which is representing count of events by useragent.&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;Next Stats:&lt;/STRONG&gt;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;|stats count("Agents Used") as "Times Used" by "Agents Used"
|table "Agents Used", "Times Used"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;CODE&gt;count("Agents Used") as "Times Used" by "Agents Used"&lt;/CODE&gt; &lt;BR /&gt;
which gives you a total number of "Agents Used" in "Times Used". And it is an invalid count of agents.&lt;/P&gt;

&lt;P&gt;If you want a count of events per Agent then I suggest below search.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=main sourcetype=access_combined_wcookie
|stats count as "Times Used" by useragent 
| rename useragent as "Agents Used"
|table "Agents Used", "Times Used"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jul 2018 08:03:04 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/stats-value-count-by-in-two-different-result/m-p/433127#M168361</guid>
      <dc:creator>kamlesh_vaghela</dc:creator>
      <dc:date>2018-07-12T08:03:04Z</dc:date>
    </item>
    <item>
      <title>Re: stats value count by in two different result</title>
      <link>https://community.splunk.com/t5/Splunk-Search/stats-value-count-by-in-two-different-result/m-p/433128#M168362</link>
      <description>&lt;P&gt;The results are different, because those SPL count something completly different.&lt;BR /&gt;
Look at the first SPL:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=main sourcetype=access_combined_wcookie
 |stats values(useragent) as "Agents Used" count as "Times Used" by useragent
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;STRONG&gt;count&lt;/STRONG&gt; counts how many times a unique useragent appeared in searched events and I suppose that is what you want to achieve&lt;/P&gt;

&lt;P&gt;...but in the second SPL:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=main sourcetype=access_combined_wcookie
 |stats values(useragent) as "Agents Used" 
 |stats count("Agents Used") as "Times Used" by "Agents Used"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;STRONG&gt;count&lt;/STRONG&gt; counts how many useragents were listed by previous command, because it is piped....&lt;/P&gt;

&lt;P&gt;Look at the "Statistics" tab on the screen you attached: It says 2 and the result of SPL for each line is also 2.&lt;/P&gt;

&lt;P&gt;Do a simple test and compare:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=main sourcetype=access_combined_wcookie
|stats values(useragent) as x count by useragent
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;and&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; index=main sourcetype=access_combined_wcookie
|stats values(useragent) as x
| stats count(x) by x
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Regards&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jul 2018 12:53:59 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/stats-value-count-by-in-two-different-result/m-p/433128#M168362</guid>
      <dc:creator>danielwysockiar</dc:creator>
      <dc:date>2018-07-12T12:53:59Z</dc:date>
    </item>
  </channel>
</rss>

