<?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: How to count rows that have non-zero values in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-count-rows-that-have-non-zero-values/m-p/329402#M98041</link>
    <description>&lt;P&gt;Try this...&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; index=xxx sourcetype=xxx source=yyy 
| fields ITEM, DEPT, *_2017, *_2018 
| streamstats count as recno
| untable recno fieldname fieldvalue
| where fieldvalue!=0 OR NOT like(fieldname,"%2017")
| xyseries recno fieldname fieldvalue
| stats count(*_2017)  by DEPT 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;HR /&gt;

&lt;P&gt;Probably not this strategy, even if it does work.  I don't generally use&lt;CODE&gt;foreach&lt;/CODE&gt; for large files, but this seems a reasonable use case.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults | eval dept=mvrange(0,3)| mvexpand dept 
| eval george1 = 3 | eval george2 = 7 | eval george3 = 0 
| table dept george*

| foreach george* [|eval &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;=case(&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;&amp;gt;0,&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;)] 
| stats count(george*) as * by dept
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 06 Dec 2017 22:15:53 GMT</pubDate>
    <dc:creator>DalJeanis</dc:creator>
    <dc:date>2017-12-06T22:15:53Z</dc:date>
    <item>
      <title>How to count rows that have non-zero values</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-count-rows-that-have-non-zero-values/m-p/329400#M98039</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;I'm trying to count the number of rows in a field that have a non-zero value. I've used replace to do that, but I get the feeling that Splunk is counting the rows that have zero values, which is not what I want it to do.&lt;/P&gt;

&lt;P&gt;I know it's not working because I'm  running two searches that have the same query, BUT, the source is different. Upon running, I'm getting identical results i.e. the number of rows for both sourcetype is the same.&lt;/P&gt;

&lt;P&gt;Query: 1&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=xxx sourcetype=xxx source=yyy | table ITEM, DEPT, *_2017, *_2018 | replace "0" WITH "" IN 03_DEC_2017 | replace "0" WITH "" IN 04_DEC_2017 | replace "0" WITH "" IN 05_DEC_2017 | stats count(*_2017) by DEPT
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Query2:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=xxx sourcetype=xxx source=xxx| table ITEM, DEPT, *_2017, *_2018 | replace "0" WITH "" IN 03_DEC_2017 | replace "0" WITH "" IN 04_DEC_2017 | replace "0" WITH "" IN 05_DEC_2017 | stats count(*_2017) by DEPT
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I need to find  way of counting rows that are NOT "" OR ideally NOT 0.&lt;/P&gt;

&lt;P&gt;Please help!&lt;/P&gt;</description>
      <pubDate>Wed, 06 Dec 2017 16:54:42 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-count-rows-that-have-non-zero-values/m-p/329400#M98039</guid>
      <dc:creator>mahbs</dc:creator>
      <dc:date>2017-12-06T16:54:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to count rows that have non-zero values</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-count-rows-that-have-non-zero-values/m-p/329401#M98040</link>
      <description>&lt;P&gt;Try like this.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=xxx sourcetype=xxx source=yyy | table ITEM, DEPT, *_2017 
|  eval "03_DEC_2017"=if(tonumber('03_DEC_2017')=0,null(),tonumber('03_DEC_2017')) 
 |  eval "04_DEC_2017"=if(tonumber('04_DEC_2017')=0,null(),tonumber('04_DEC_2017')) 
 |  eval "05_DEC_2017"=if(tonumber('05_DEC_2017')=0,null(),tonumber('05_DEC_2017')) 
 | stats count(*_2017) by DEPT
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The value &lt;CODE&gt;""&lt;/CODE&gt; that you were using in replace is a non-null value and is being counted using &lt;CODE&gt;count&lt;/CODE&gt; function in stats. Changing it to null() would ensure it's ignored while doing stats count.&lt;/P&gt;</description>
      <pubDate>Wed, 06 Dec 2017 19:00:49 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-count-rows-that-have-non-zero-values/m-p/329401#M98040</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2017-12-06T19:00:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to count rows that have non-zero values</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-count-rows-that-have-non-zero-values/m-p/329402#M98041</link>
      <description>&lt;P&gt;Try this...&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; index=xxx sourcetype=xxx source=yyy 
| fields ITEM, DEPT, *_2017, *_2018 
| streamstats count as recno
| untable recno fieldname fieldvalue
| where fieldvalue!=0 OR NOT like(fieldname,"%2017")
| xyseries recno fieldname fieldvalue
| stats count(*_2017)  by DEPT 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;HR /&gt;

&lt;P&gt;Probably not this strategy, even if it does work.  I don't generally use&lt;CODE&gt;foreach&lt;/CODE&gt; for large files, but this seems a reasonable use case.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults | eval dept=mvrange(0,3)| mvexpand dept 
| eval george1 = 3 | eval george2 = 7 | eval george3 = 0 
| table dept george*

| foreach george* [|eval &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;=case(&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;&amp;gt;0,&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;)] 
| stats count(george*) as * by dept
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 06 Dec 2017 22:15:53 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-count-rows-that-have-non-zero-values/m-p/329402#M98041</guid>
      <dc:creator>DalJeanis</dc:creator>
      <dc:date>2017-12-06T22:15:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to count rows that have non-zero values</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-count-rows-that-have-non-zero-values/m-p/329403#M98042</link>
      <description>&lt;P&gt;I always check &lt;CODE&gt;len(fieldname)&amp;gt;0&lt;/CODE&gt; for this kind of thing because it handles all varieties of blankishness.&lt;/P&gt;</description>
      <pubDate>Fri, 08 Dec 2017 07:25:48 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-count-rows-that-have-non-zero-values/m-p/329403#M98042</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2017-12-08T07:25:48Z</dc:date>
    </item>
  </channel>
</rss>

