<?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 Streamstats and resetting a running total in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Streamstats-and-resetting-a-running-total/m-p/191597#M55166</link>
    <description>&lt;P&gt;I'm using &lt;CODE&gt;streamstats&lt;/CODE&gt; to calculate the running total for a value&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | streamstats sum(amount) as cumulativeAmount
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;But I need to reset the running total whenever a particular event is encountered and I can't seem to figure out how.&lt;/P&gt;

&lt;P&gt;An example of the output I'm trying to achieve:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;amount=10 cumulativeAmount=10
amount=12 cumulativeAmount=22
amount=4 cumulativeAmount=26
reset cumulativeAmount=0
amount=5 cumulativeAmount=5
reset cumulativeAmount=0
amount=8 cumulativeAmount=8
amount=11 cumulativeAmount=19
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I've tried this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | eval cumulativeAmount=case(someCriteria, 0) | streamstats sum(amount) as cumulativeAmount
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;But &lt;CODE&gt;streamstats&lt;/CODE&gt; overwrites the &lt;CODE&gt;cumulativeAmount&lt;/CODE&gt; set by the &lt;CODE&gt;eval&lt;/CODE&gt;. I've also tried something like:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | streamstats sum(eval(case(someCriteria1, amount, someCriteria2, latest(cumulativeAmount)*-1))) as cumulativeAmount
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;But Splunk complains that &lt;CODE&gt;latest&lt;/CODE&gt; isn't a function you can use inside a &lt;CODE&gt;case&lt;/CODE&gt;. Any other way of doing this would be fine, doesn't need to be with &lt;CODE&gt;streamstats&lt;/CODE&gt;. Any ideas?&lt;/P&gt;</description>
    <pubDate>Fri, 06 Jun 2014 20:27:12 GMT</pubDate>
    <dc:creator>curtisb1024</dc:creator>
    <dc:date>2014-06-06T20:27:12Z</dc:date>
    <item>
      <title>Streamstats and resetting a running total</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Streamstats-and-resetting-a-running-total/m-p/191597#M55166</link>
      <description>&lt;P&gt;I'm using &lt;CODE&gt;streamstats&lt;/CODE&gt; to calculate the running total for a value&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | streamstats sum(amount) as cumulativeAmount
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;But I need to reset the running total whenever a particular event is encountered and I can't seem to figure out how.&lt;/P&gt;

&lt;P&gt;An example of the output I'm trying to achieve:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;amount=10 cumulativeAmount=10
amount=12 cumulativeAmount=22
amount=4 cumulativeAmount=26
reset cumulativeAmount=0
amount=5 cumulativeAmount=5
reset cumulativeAmount=0
amount=8 cumulativeAmount=8
amount=11 cumulativeAmount=19
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I've tried this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | eval cumulativeAmount=case(someCriteria, 0) | streamstats sum(amount) as cumulativeAmount
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;But &lt;CODE&gt;streamstats&lt;/CODE&gt; overwrites the &lt;CODE&gt;cumulativeAmount&lt;/CODE&gt; set by the &lt;CODE&gt;eval&lt;/CODE&gt;. I've also tried something like:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | streamstats sum(eval(case(someCriteria1, amount, someCriteria2, latest(cumulativeAmount)*-1))) as cumulativeAmount
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;But Splunk complains that &lt;CODE&gt;latest&lt;/CODE&gt; isn't a function you can use inside a &lt;CODE&gt;case&lt;/CODE&gt;. Any other way of doing this would be fine, doesn't need to be with &lt;CODE&gt;streamstats&lt;/CODE&gt;. Any ideas?&lt;/P&gt;</description>
      <pubDate>Fri, 06 Jun 2014 20:27:12 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Streamstats-and-resetting-a-running-total/m-p/191597#M55166</guid>
      <dc:creator>curtisb1024</dc:creator>
      <dc:date>2014-06-06T20:27:12Z</dc:date>
    </item>
    <item>
      <title>Re: Streamstats and resetting a running total</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Streamstats-and-resetting-a-running-total/m-p/191598#M55167</link>
      <description>&lt;P&gt;Try something like this&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;your base search | eval reset=if(someCriteria,1,0) | accum reset  | streamstats sum(amount) as cumulativeAmount by reset
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I am adding a field reset value of which will be 1 for the event where reset is happening and 0 otherwise. After that, accum command will do the commulative addition of reset and create a series like 0,0,0...until first reset, then 1,1,1,...until second reset, then 3,3,3... and so on. Then the streamstats can create commulative sum based on the reset as the events are now categorized for each reset.&lt;/P&gt;</description>
      <pubDate>Fri, 06 Jun 2014 21:05:25 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Streamstats-and-resetting-a-running-total/m-p/191598#M55167</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2014-06-06T21:05:25Z</dc:date>
    </item>
    <item>
      <title>Re: Streamstats and resetting a running total</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Streamstats-and-resetting-a-running-total/m-p/191599#M55168</link>
      <description>&lt;P&gt;That did the trick. Thanks!&lt;/P&gt;</description>
      <pubDate>Sat, 07 Jun 2014 01:09:46 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Streamstats-and-resetting-a-running-total/m-p/191599#M55168</guid>
      <dc:creator>curtisb1024</dc:creator>
      <dc:date>2014-06-07T01:09:46Z</dc:date>
    </item>
    <item>
      <title>Re: Streamstats and resetting a running total</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Streamstats-and-resetting-a-running-total/m-p/191600#M55169</link>
      <description>&lt;P&gt;I tried this, but the criteria I need to check on has not been established and the variable from streamstats (cumulativeAmount) which could be used to establish the criteria, does not appear to be available in the eval statement. It passes the isnull(cumulativeAmount) check every time.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Aug 2015 13:36:46 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Streamstats-and-resetting-a-running-total/m-p/191600#M55169</guid>
      <dc:creator>nmaiorana</dc:creator>
      <dc:date>2015-08-20T13:36:46Z</dc:date>
    </item>
  </channel>
</rss>

