<?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: Streamstats sum that doesn't go below zero in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Streamstats-sum-that-doesn-t-go-below-zero/m-p/454373#M128561</link>
    <description>&lt;P&gt;David....there are no words to describe how grateful I am!&lt;/P&gt;

&lt;P&gt;That appears to have done the trick! Thank you so much! I've been racking my brain over that one for hours. I was all over the reset_after etc. too!&lt;/P&gt;

&lt;P&gt;Have a great day!&lt;/P&gt;</description>
    <pubDate>Tue, 20 Aug 2019 14:06:42 GMT</pubDate>
    <dc:creator>ALXWBR</dc:creator>
    <dc:date>2019-08-20T14:06:42Z</dc:date>
    <item>
      <title>Streamstats sum that doesn't go below zero</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Streamstats-sum-that-doesn-t-go-below-zero/m-p/454371#M128559</link>
      <description>&lt;P&gt;This is the second time I have come across this problem but I really can't seem to find any answer anywhere. I need to streamstats sum a number field that has positive and negative integers, but I don't want the answer to go below zero. Fore example:&lt;/P&gt;

&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="alt text"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/7535i3836AC87A3815312/image-size/large?v=v2&amp;amp;px=999" role="button" title="alt text" alt="alt text" /&gt;&lt;/span&gt;&lt;/P&gt;

&lt;P&gt;If the FIELD column represents the values I want to sum, then currently a streamstats sum(FIELD) command produces results in the CURRENT column, where as, I need it to calculate as the DESIRED column.&lt;/P&gt;

&lt;P&gt;This is because I am reviewing capacity usage and I can't have it negatively used.&lt;/P&gt;

&lt;P&gt;PLEASE HELP!!!&lt;/P&gt;</description>
      <pubDate>Tue, 20 Aug 2019 12:35:47 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Streamstats-sum-that-doesn-t-go-below-zero/m-p/454371#M128559</guid>
      <dc:creator>ALXWBR</dc:creator>
      <dc:date>2019-08-20T12:35:47Z</dc:date>
    </item>
    <item>
      <title>Re: Streamstats sum that doesn't go below zero</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Streamstats-sum-that-doesn-t-go-below-zero/m-p/454372#M128560</link>
      <description>&lt;P&gt;Hi @ALXWBR,&lt;/P&gt;

&lt;P&gt;Wow that's a real mind twister... Made me scratch my head for this one... &lt;/P&gt;

&lt;P&gt;At first I thought that using &lt;CODE&gt;reset_after&lt;/CODE&gt; or &lt;CODE&gt;reset before&lt;/CODE&gt; option from &lt;CODE&gt;streamstats&lt;/CODE&gt; would help, but they don't make much sense since your case doesn't really follow a fixed &lt;CODE&gt;streamstats&lt;/CODE&gt; logic. It's really a maths problem. &lt;/P&gt;

&lt;P&gt;So here's how I think you can solve it for negative values :&lt;/P&gt;

&lt;P&gt;Step 1: Run a search to get the FIELD and current column that you have in your table above.&lt;/P&gt;

&lt;P&gt;Step 2: Generate a new field containing the running minimum value of of your CURRENT field. This means anytime there's a new minimum for CURRENT it will be saved, this field will be used as the "real zero" field in step 3. You can do that using &lt;CODE&gt;|streamstats min(CURRENT) as minCURRENT&lt;/CODE&gt;by this step you have "FIELD", "CURRENT" and "minCURRENT" lined up.&lt;/P&gt;

&lt;P&gt;Step 3: Your minCURRENT is now the real 0 for all negative values, so anytime your anytime the minCURRENT is equal to CURRENT you should replace it with 0. And any time CURRENT is negative and larger than minCURRENT you should replace it with the diff of both values. &lt;BR /&gt;
This translates to that : &lt;CODE&gt;....| eval DESIRED= case(CURRENT== minCURRENT, 0, CURRENT&amp;lt;0 AND CURRENT&amp;gt;minCURRENT, CURRENT-minCURRENT, CURRENT&amp;gt;0 AND CURRENT&amp;lt; FIELD, FIELD, 1=1 , CURRENT)&lt;/CODE&gt;&lt;BR /&gt;
That last part : &lt;CODE&gt;CURRENT&amp;gt;0 AND CURRENT&amp;lt; FIELD, FIELD, 1=1 , CURRENT&lt;/CODE&gt; handles values when CURRENT is positive. &lt;/P&gt;

&lt;P&gt;Let me know if you need more details or help, this isn't very easy I have to admit.&lt;/P&gt;

&lt;P&gt;Cheers,&lt;BR /&gt;
David&lt;/P&gt;</description>
      <pubDate>Tue, 20 Aug 2019 13:48:11 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Streamstats-sum-that-doesn-t-go-below-zero/m-p/454372#M128560</guid>
      <dc:creator>DavidHourani</dc:creator>
      <dc:date>2019-08-20T13:48:11Z</dc:date>
    </item>
    <item>
      <title>Re: Streamstats sum that doesn't go below zero</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Streamstats-sum-that-doesn-t-go-below-zero/m-p/454373#M128561</link>
      <description>&lt;P&gt;David....there are no words to describe how grateful I am!&lt;/P&gt;

&lt;P&gt;That appears to have done the trick! Thank you so much! I've been racking my brain over that one for hours. I was all over the reset_after etc. too!&lt;/P&gt;

&lt;P&gt;Have a great day!&lt;/P&gt;</description>
      <pubDate>Tue, 20 Aug 2019 14:06:42 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Streamstats-sum-that-doesn-t-go-below-zero/m-p/454373#M128561</guid>
      <dc:creator>ALXWBR</dc:creator>
      <dc:date>2019-08-20T14:06:42Z</dc:date>
    </item>
    <item>
      <title>Re: Streamstats sum that doesn't go below zero</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Streamstats-sum-that-doesn-t-go-below-zero/m-p/454374#M128562</link>
      <description>&lt;P&gt;You're most welcome ! It's really tricky... felt like solving one of those brain teasers lol &lt;/P&gt;

&lt;P&gt;Keep a lookout for the change from negative to positive though, not sure how well that case is handled so let me know if you notice anything strange about it &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Aug 2019 19:00:51 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Streamstats-sum-that-doesn-t-go-below-zero/m-p/454374#M128562</guid>
      <dc:creator>DavidHourani</dc:creator>
      <dc:date>2019-08-20T19:00:51Z</dc:date>
    </item>
    <item>
      <title>Re: Streamstats sum that doesn't go below zero</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Streamstats-sum-that-doesn-t-go-below-zero/m-p/513931#M144241</link>
      <description>&lt;P&gt;As I detailed at&amp;nbsp;&lt;A href="https://community.splunk.com/t5/Splunk-Search/Case-open-count-and-trend-history/m-p/513894/highlight/false" target="_blank"&gt;https://community.splunk.com/t5/Splunk-Search/Case-open-count-and-trend-history/m-p/513894/highlight/false&lt;/A&gt;, this is a great solution but has one problem: it incorrectly handles the field "CURRENT" having a value of 0.&lt;/P&gt;&lt;P&gt;To work around this, I simply used something like "| append [| makeresults | eval CURRENT = -10000] | reverse" which sets a baseline so low that CURRENT should never even get to 0 but everything else still works.&lt;/P&gt;</description>
      <pubDate>Thu, 13 Aug 2020 14:54:27 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Streamstats-sum-that-doesn-t-go-below-zero/m-p/513931#M144241</guid>
      <dc:creator>benhooper</dc:creator>
      <dc:date>2020-08-13T14:54:27Z</dc:date>
    </item>
  </channel>
</rss>

