<?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 can I use the output of streamstats in current row, and have it feed back into streamstats for next row? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-use-the-output-of-streamstats-in-current-row-and-have/m-p/395707#M114889</link>
    <description>&lt;P&gt;Okay, you are thinking about this the wrong way.  You are "committing spreadsheet".&lt;/P&gt;

&lt;P&gt;The value you want is the running total, right?  Which is the net total from EVERYTHING that has gone before.  &lt;/P&gt;

&lt;P&gt;You will have to figure out what is appropriate to get the initial running balance (Startcount), but the rest is trivial.  Calculate the net-addition onto the record, and then use streamstats to calculate the total net running balance for each record.&lt;/P&gt;

&lt;P&gt;Here's a run-anywhere example...&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults count=10 

| rename COMMENT as "Put StartCount only on the first record, put updates on all other records"
| streamstats count as recno
| eval  StartCount = case(recno==1,63)
| eval fieldA = case(recno!=1,random() % 10)
| eval fieldB = case(recno!=1,0-(random() % 10))
| fields- _time recno

| rename COMMENT as "Calculate net change"
| eval RunningNet=coalesce(StartCount, (fieldA+fieldB) ,0)

| rename COMMENT as "Calculate running totals"
| streamstats sum(RunningNet) as RunningTotal
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;HR /&gt;

&lt;P&gt;When you find yourself stuck in a paradigm like this, it's best to step back a few feet and look at what you started with, rather than what you think you need to use from where your code happens to be.  Often, the correct approach in Splunk starts with viewing the incoming events in a different light.&lt;/P&gt;</description>
    <pubDate>Tue, 18 Sep 2018 18:06:51 GMT</pubDate>
    <dc:creator>DalJeanis</dc:creator>
    <dc:date>2018-09-18T18:06:51Z</dc:date>
    <item>
      <title>How can I use the output of streamstats in current row, and have it feed back into streamstats for next row?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-use-the-output-of-streamstats-in-current-row-and-have/m-p/395706#M114888</link>
      <description>&lt;P&gt;I need to calculate a running total, which uses two values from the previous row (one being this calculated total), and adds on a value from this row to create the total. It's slightly hard to explain, but fairly straightforward.&lt;/P&gt;

&lt;P&gt;Imagine I have 3 fields, &lt;CODE&gt;StartCount&lt;/CODE&gt; (never changes), &lt;CODE&gt;fieldA&lt;/CODE&gt; (always &amp;gt;=0) and &lt;CODE&gt;fieldB&lt;/CODE&gt; (always &amp;lt;= 0). I need to calculate &lt;CODE&gt;RunningTotal&lt;/CODE&gt;, where &lt;BR /&gt;
&lt;CODE&gt;RunningTotal = &amp;lt;previousRow&amp;gt;RunningTotal - &amp;lt;previousRow&amp;gt;fieldA - &amp;lt;currentRow&amp;gt;fieldB&lt;/CODE&gt;&lt;BR /&gt;
On the first row, &lt;CODE&gt;RunningTotal&lt;/CODE&gt; should be set to &lt;CODE&gt;StartCount&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;So this is my desired output:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| StartCount | RunningTotal | fieldA | fieldB |    
+------------+--------------+--------+--------+
|         63 |           63 |      8 |     -3 |     &amp;lt;- RunningTotal = StartCount
|         63 |           57 |      6 |     -2 |     &amp;lt;- RunningTotal = 63 - 8 - -2
|         63 |           59 |      2 |     -8 |     &amp;lt;- RunningTotal = 57 - 6 - -8
|         63 |           64 |      6 |     -7 |     &amp;lt;- RunningTotal = 59 - 2 - -7
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;So this is the SPL I've tried using:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults count=10 
 | fields- _time
 | eval StartCount = 63
 | eval fieldA = random() % 10
 | eval fieldB = 0-(random() % 10)

 | streamstats current=f window=1 last(fieldA) as prev_fieldA last(RunningTotal) as prev_RunningTotal 
 | eval prev_RunningTotal  = if( isnull(prev_RunningTotal ), StartCount, prev_RunningTotal )

 | eval RunningTotal = if( isnull(prev_fieldA), StartCount, prev_RunningTotal  - prev_fieldA - fieldB )     
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The problem I have is that prev_RunningTotal never changes, it's always equal to StartCount.&lt;/P&gt;

&lt;P&gt;Can I not access previous &lt;CODE&gt;streamstats&lt;/CODE&gt; outputs within the &lt;CODE&gt;streamstats&lt;/CODE&gt;?  Or is there another way to solve this, when I don't know how many events/rows will be returned to carry out the total operation for?&lt;/P&gt;</description>
      <pubDate>Wed, 08 Aug 2018 10:35:19 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-use-the-output-of-streamstats-in-current-row-and-have/m-p/395706#M114888</guid>
      <dc:creator>philtanner</dc:creator>
      <dc:date>2018-08-08T10:35:19Z</dc:date>
    </item>
    <item>
      <title>Re: How can I use the output of streamstats in current row, and have it feed back into streamstats for next row?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-use-the-output-of-streamstats-in-current-row-and-have/m-p/395707#M114889</link>
      <description>&lt;P&gt;Okay, you are thinking about this the wrong way.  You are "committing spreadsheet".&lt;/P&gt;

&lt;P&gt;The value you want is the running total, right?  Which is the net total from EVERYTHING that has gone before.  &lt;/P&gt;

&lt;P&gt;You will have to figure out what is appropriate to get the initial running balance (Startcount), but the rest is trivial.  Calculate the net-addition onto the record, and then use streamstats to calculate the total net running balance for each record.&lt;/P&gt;

&lt;P&gt;Here's a run-anywhere example...&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults count=10 

| rename COMMENT as "Put StartCount only on the first record, put updates on all other records"
| streamstats count as recno
| eval  StartCount = case(recno==1,63)
| eval fieldA = case(recno!=1,random() % 10)
| eval fieldB = case(recno!=1,0-(random() % 10))
| fields- _time recno

| rename COMMENT as "Calculate net change"
| eval RunningNet=coalesce(StartCount, (fieldA+fieldB) ,0)

| rename COMMENT as "Calculate running totals"
| streamstats sum(RunningNet) as RunningTotal
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;HR /&gt;

&lt;P&gt;When you find yourself stuck in a paradigm like this, it's best to step back a few feet and look at what you started with, rather than what you think you need to use from where your code happens to be.  Often, the correct approach in Splunk starts with viewing the incoming events in a different light.&lt;/P&gt;</description>
      <pubDate>Tue, 18 Sep 2018 18:06:51 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-use-the-output-of-streamstats-in-current-row-and-have/m-p/395707#M114889</guid>
      <dc:creator>DalJeanis</dc:creator>
      <dc:date>2018-09-18T18:06:51Z</dc:date>
    </item>
  </channel>
</rss>

