<?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 calculate only specific fields in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-only-specific-fields/m-p/354295#M104879</link>
    <description>&lt;P&gt;Here's some run-anywhere code to show you how to get what you want.  It's only complicated because of your not wanting the accumulated totals to go past zero records, and not wanting it to appear except on the last record of each group.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults count=20 | streamstats count as recno | eval _time=relative_time(now(),"-2h@h")+600*recno 
| eval rand=((random()%7)+(random()%37))%2 | eval A=if(rand&amp;gt;0,10*rand,null()) | eval B=if(rand&amp;gt;0,null(),10*(1-rand))
| table _time A B
| rename COMMENT as "The above just makes some random test data."

| rename COMMENT as "Now we break the transactions up into groups, considering A and B separately because it's easier."
| rename COMMENT as "Each group of actual values for A (for example) gets the tranA for the null A record before it."
| streamstats count(eval(isnull(A))) as tranA, count(eval(isnull(B))) as tranB
| eventstats sum(A) as Asum by tranA
| eventstats sum(B) as Bsum by tranB

| rename COMMENT as "We sort them into reverse time order, to identify the last event in each group, and blank the sums for every other event."
| sort 0 - _time
| autoregress tranA as nextA P=1
| autoregress tranB as nextB P=1
| eval Asum=if(A=0 OR tranA=nextA,null(),Asum)
| eval Bsum=if(B=0 OR tranB=nextB,null(),Bsum)

| rename COMMENT as "We flip them back into _time order, and eliminate all the work fields."
| reverse
| table _time A B Asum Bsum
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 01 May 2017 16:20:40 GMT</pubDate>
    <dc:creator>DalJeanis</dc:creator>
    <dc:date>2017-05-01T16:20:40Z</dc:date>
    <item>
      <title>How to calculate only specific fields</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-only-specific-fields/m-p/354292#M104876</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;

&lt;P&gt;from my raw data:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;TIME                A   B   
2017-04-26 13:00:00     10         
2017-04-26 13:10:00 10   
2017-04-26 13:20:00 10      
2017-04-26 13:30:00     10
2017-04-26 13:40:00     10
2017-04-26 13:50:00     10
2017-04-26 14:00:00     10
2017-04-26 14:10:00     10
2017-04-26 14:20:00     10         
2017-04-26 14:30:00 10   
2017-04-26 14:40:00 10      
2017-04-26 14:50:00     10
2017-04-26 15:00:00     10
2017-04-26 15:10:00     10
2017-04-26 15:20:00     10
2017-04-26 15:30:00     10
2017-04-26 15:40:00     10
2017-04-26 15:50:00     10
2017-04-26 16:00:00     10
2017-04-26 16:10:00     10
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;i would like to achieve following output:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;TIME                A   B   Asum   Bsum          
2017-04-26 13:00:00     10       10
2017-04-26 13:10:00 10   
2017-04-26 13:20:00 10   20
2017-04-26 13:30:00     10
2017-04-26 13:40:00     10
2017-04-26 13:50:00     10
2017-04-26 14:00:00     10
2017-04-26 14:10:00     10
2017-04-26 14:20:00     10         60
2017-04-26 14:30:00 10   
2017-04-26 14:40:00 10    20
2017-04-26 14:50:00     10
2017-04-26 15:00:00     10
2017-04-26 15:10:00     10
2017-04-26 15:20:00     10
2017-04-26 15:30:00     10
2017-04-26 15:40:00     10
2017-04-26 15:50:00     10
2017-04-26 16:00:00     10
2017-04-26 16:10:00     10         90
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;How can it be done?&lt;/P&gt;

&lt;P&gt;Best regards&lt;BR /&gt;
Tomasz&lt;/P&gt;</description>
      <pubDate>Fri, 28 Apr 2017 11:24:39 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-only-specific-fields/m-p/354292#M104876</guid>
      <dc:creator>tomaszwrona</dc:creator>
      <dc:date>2017-04-28T11:24:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate only specific fields</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-only-specific-fields/m-p/354293#M104877</link>
      <description>&lt;P&gt;How about the following?&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| timechart span=10m sum(A) as  A sum(B) as B
| accum A as CumulativeA
| accum B as CumulativeB
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;You can either create a Chart overlay with CumulativeA and CumulativeB or show only Cumulative fields.&lt;/P&gt;</description>
      <pubDate>Sat, 29 Apr 2017 06:10:28 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-only-specific-fields/m-p/354293#M104877</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2017-04-29T06:10:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate only specific fields</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-only-specific-fields/m-p/354294#M104878</link>
      <description>&lt;P&gt;you could try streamstats. i think the below might work but one of the other options for streamstats might work better&lt;/P&gt;

&lt;P&gt;&lt;A href="https://docs.splunk.com/Documentation/Splunk/6.5.3/SearchReference/Streamstats"&gt;https://docs.splunk.com/Documentation/Splunk/6.5.3/SearchReference/Streamstats&lt;/A&gt;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;|streamstats reset_after="("isnull(A)") sum(A) as sumA|streamstats reset_after="("isnull(B)") sum(B) as sumB
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 01 May 2017 12:12:31 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-only-specific-fields/m-p/354294#M104878</guid>
      <dc:creator>cmerriman</dc:creator>
      <dc:date>2017-05-01T12:12:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate only specific fields</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-only-specific-fields/m-p/354295#M104879</link>
      <description>&lt;P&gt;Here's some run-anywhere code to show you how to get what you want.  It's only complicated because of your not wanting the accumulated totals to go past zero records, and not wanting it to appear except on the last record of each group.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults count=20 | streamstats count as recno | eval _time=relative_time(now(),"-2h@h")+600*recno 
| eval rand=((random()%7)+(random()%37))%2 | eval A=if(rand&amp;gt;0,10*rand,null()) | eval B=if(rand&amp;gt;0,null(),10*(1-rand))
| table _time A B
| rename COMMENT as "The above just makes some random test data."

| rename COMMENT as "Now we break the transactions up into groups, considering A and B separately because it's easier."
| rename COMMENT as "Each group of actual values for A (for example) gets the tranA for the null A record before it."
| streamstats count(eval(isnull(A))) as tranA, count(eval(isnull(B))) as tranB
| eventstats sum(A) as Asum by tranA
| eventstats sum(B) as Bsum by tranB

| rename COMMENT as "We sort them into reverse time order, to identify the last event in each group, and blank the sums for every other event."
| sort 0 - _time
| autoregress tranA as nextA P=1
| autoregress tranB as nextB P=1
| eval Asum=if(A=0 OR tranA=nextA,null(),Asum)
| eval Bsum=if(B=0 OR tranB=nextB,null(),Bsum)

| rename COMMENT as "We flip them back into _time order, and eliminate all the work fields."
| reverse
| table _time A B Asum Bsum
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 01 May 2017 16:20:40 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-only-specific-fields/m-p/354295#M104879</guid>
      <dc:creator>DalJeanis</dc:creator>
      <dc:date>2017-05-01T16:20:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate only specific fields</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-only-specific-fields/m-p/354296#M104880</link>
      <description>&lt;P&gt;for my purpose this is the best way - thank you!&lt;/P&gt;</description>
      <pubDate>Tue, 02 May 2017 06:52:47 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-only-specific-fields/m-p/354296#M104880</guid>
      <dc:creator>tomaszwrona</dc:creator>
      <dc:date>2017-05-02T06:52:47Z</dc:date>
    </item>
  </channel>
</rss>

