<?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 Conditional count in stats function in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Conditional-count-in-stats-function/m-p/171592#M49181</link>
    <description>&lt;P&gt;I need to do the following:&lt;/P&gt;

&lt;P&gt;Get a distinct count of serial numbers where a selected date falls within a particular range. Figuring out if a serial number is valid per table row is easy:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;eval inService=if(enteredService&amp;lt;beginTime AND (isnull(leftService) OR leftService&amp;gt;beginTime),1,0)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The problem is I want to sum up all of these 1s but only once for each distinct serial number (a serial number can appear in the table multiple times).&lt;/P&gt;

&lt;P&gt;Currently my stats line looks like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;stats sum(quarterPortion) AS sumQuarter dc(serialNumber) AS countSerNum by modelClass
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I'm trying to get a ratio between sumQuarter (the amount something is used) and countSerNum (the number of things available for use). This is set up so that I can look at any historical quarter for sumQuarter, but sadly dc(serialNumber) only gives me the current count, not the historical count for the selected timeframe.&lt;/P&gt;

&lt;P&gt;This is coming from a database query, so I can't just restrict the time range ahead of the search. I want to do something like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;dc(eval if(enteredService&amp;lt;beginTime AND (isnull(leftService) OR leftService&amp;gt;beginTime),serNum))
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Obviously this doesn't exactly work.&lt;/P&gt;</description>
    <pubDate>Tue, 05 Aug 2014 17:51:52 GMT</pubDate>
    <dc:creator>willial</dc:creator>
    <dc:date>2014-08-05T17:51:52Z</dc:date>
    <item>
      <title>Conditional count in stats function</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Conditional-count-in-stats-function/m-p/171592#M49181</link>
      <description>&lt;P&gt;I need to do the following:&lt;/P&gt;

&lt;P&gt;Get a distinct count of serial numbers where a selected date falls within a particular range. Figuring out if a serial number is valid per table row is easy:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;eval inService=if(enteredService&amp;lt;beginTime AND (isnull(leftService) OR leftService&amp;gt;beginTime),1,0)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The problem is I want to sum up all of these 1s but only once for each distinct serial number (a serial number can appear in the table multiple times).&lt;/P&gt;

&lt;P&gt;Currently my stats line looks like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;stats sum(quarterPortion) AS sumQuarter dc(serialNumber) AS countSerNum by modelClass
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I'm trying to get a ratio between sumQuarter (the amount something is used) and countSerNum (the number of things available for use). This is set up so that I can look at any historical quarter for sumQuarter, but sadly dc(serialNumber) only gives me the current count, not the historical count for the selected timeframe.&lt;/P&gt;

&lt;P&gt;This is coming from a database query, so I can't just restrict the time range ahead of the search. I want to do something like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;dc(eval if(enteredService&amp;lt;beginTime AND (isnull(leftService) OR leftService&amp;gt;beginTime),serNum))
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Obviously this doesn't exactly work.&lt;/P&gt;</description>
      <pubDate>Tue, 05 Aug 2014 17:51:52 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Conditional-count-in-stats-function/m-p/171592#M49181</guid>
      <dc:creator>willial</dc:creator>
      <dc:date>2014-08-05T17:51:52Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional count in stats function</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Conditional-count-in-stats-function/m-p/171593#M49182</link>
      <description>&lt;P&gt;May be something like this...&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;your base search | eval inService=if(enteredService&amp;lt;beginTime AND (isnull(leftService) OR leftService&amp;gt;beginTime),1,0) | stats sum(quarterPortion) AS sumQuarter dc(serialNumber) AS countSerNum by modelClass,inService |eval countSerNum=countSerNum*inService | stats sum(sumQuarter) as sumQuarter max(countSerNum) as countSerNum by modelClass
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;OR &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;your base search | eval serialNumber=if(enteredService&amp;lt;beginTime AND (isnull(leftService) OR leftService&amp;gt;beginTime),serialNumber,null())| stats sum(quarterPortion) AS sumQuarter dc(serialNumber) AS countSerNum by modelClass
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 05 Aug 2014 18:11:31 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Conditional-count-in-stats-function/m-p/171593#M49182</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2014-08-05T18:11:31Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional count in stats function</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Conditional-count-in-stats-function/m-p/171594#M49183</link>
      <description>&lt;P&gt;I think #2 is working properly (as far as I can tell). I'll revisit this if I discover I'm wrong. Otherwise, high marks for cleverness.&lt;/P&gt;</description>
      <pubDate>Tue, 05 Aug 2014 18:48:03 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Conditional-count-in-stats-function/m-p/171594#M49183</guid>
      <dc:creator>willial</dc:creator>
      <dc:date>2014-08-05T18:48:03Z</dc:date>
    </item>
  </channel>
</rss>

