<?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 What is the best way to add a set of values in a table, subtract another set of values, then append the result back to the table? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/What-is-the-best-way-to-add-a-set-of-values-in-a-table-subtract/m-p/204430#M59379</link>
    <description>&lt;P&gt;I have the following situation:&lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;some commands | table Type, Value&lt;/CODE&gt; which results in:&lt;/P&gt;

&lt;H1&gt;Type, Value&lt;/H1&gt;

&lt;P&gt;A, 5&lt;BR /&gt;
B, 5&lt;BR /&gt;
C, 1&lt;BR /&gt;
D, 0&lt;/P&gt;

&lt;P&gt;I need to add up A and B, subtract C and D, then append the result back to the table like this as another value Type="E":&lt;/P&gt;

&lt;H1&gt;Type, Value&lt;/H1&gt;

&lt;P&gt;A, 5&lt;BR /&gt;
B, 5&lt;BR /&gt;
E, 9 &amp;lt;- 5+5-(1+0)&lt;/P&gt;

&lt;P&gt;Any ideas?&lt;/P&gt;</description>
    <pubDate>Tue, 16 Feb 2016 08:57:36 GMT</pubDate>
    <dc:creator>Stevelim</dc:creator>
    <dc:date>2016-02-16T08:57:36Z</dc:date>
    <item>
      <title>What is the best way to add a set of values in a table, subtract another set of values, then append the result back to the table?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/What-is-the-best-way-to-add-a-set-of-values-in-a-table-subtract/m-p/204430#M59379</link>
      <description>&lt;P&gt;I have the following situation:&lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;some commands | table Type, Value&lt;/CODE&gt; which results in:&lt;/P&gt;

&lt;H1&gt;Type, Value&lt;/H1&gt;

&lt;P&gt;A, 5&lt;BR /&gt;
B, 5&lt;BR /&gt;
C, 1&lt;BR /&gt;
D, 0&lt;/P&gt;

&lt;P&gt;I need to add up A and B, subtract C and D, then append the result back to the table like this as another value Type="E":&lt;/P&gt;

&lt;H1&gt;Type, Value&lt;/H1&gt;

&lt;P&gt;A, 5&lt;BR /&gt;
B, 5&lt;BR /&gt;
E, 9 &amp;lt;- 5+5-(1+0)&lt;/P&gt;

&lt;P&gt;Any ideas?&lt;/P&gt;</description>
      <pubDate>Tue, 16 Feb 2016 08:57:36 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/What-is-the-best-way-to-add-a-set-of-values-in-a-table-subtract/m-p/204430#M59379</guid>
      <dc:creator>Stevelim</dc:creator>
      <dc:date>2016-02-16T08:57:36Z</dc:date>
    </item>
    <item>
      <title>Re: What is the best way to add a set of values in a table, subtract another set of values, then append the result back to the table?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/What-is-the-best-way-to-add-a-set-of-values-in-a-table-subtract/m-p/204431#M59380</link>
      <description>&lt;P&gt;Is there any categorization available for A,B and C,D or any other field which can be used to group A,B and C,D?&lt;/P&gt;</description>
      <pubDate>Tue, 16 Feb 2016 09:31:51 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/What-is-the-best-way-to-add-a-set-of-values-in-a-table-subtract/m-p/204431#M59380</guid>
      <dc:creator>renjith_nair</dc:creator>
      <dc:date>2016-02-16T09:31:51Z</dc:date>
    </item>
    <item>
      <title>Re: What is the best way to add a set of values in a table, subtract another set of values, then append the result back to the table?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/What-is-the-best-way-to-add-a-set-of-values-in-a-table-subtract/m-p/204432#M59381</link>
      <description>&lt;P&gt;Appendpipe is one solution. It allows you to append to reporting commands. In this case, instead of using &lt;CODE&gt;table Type Value&lt;/CODE&gt; use &lt;CODE&gt;stats&lt;/CODE&gt;to sum the values of your types, then use &lt;CODE&gt;eval&lt;/CODE&gt; to convert C &amp;amp; D to negative values. Then use &lt;CODE&gt;appendpipe&lt;/CODE&gt; to get the total of your column&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... |stats sum(Value) as Value by Type | eval Value=if((type=="C" OR type=="D"),Value*-1,Value) | appendpipe [stats sum(Value) as Value | eval Type = "E"] | search NOT (Type="C" OR Type="D")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;You can also use addcoltotals (a simpler method, but appendpipe allows you to do more than simple sum, so it's worth knowing)&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... |stats sum(Value) as Value by Type | eval Value=if((type=="C" OR type=="D"),Value*-1,Value) | addcoltotals Value labelfield=Type label=E | search NOT (Type="C" OR Type="D")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;See: &lt;A href="http://docs.splunk.com/Documentation/Splunk/6.3.3/SearchReference/Appendpipe"&gt;http://docs.splunk.com/Documentation/Splunk/6.3.3/SearchReference/Appendpipe&lt;/A&gt;&lt;BR /&gt;
See: &lt;A href="http://docs.splunk.com/Documentation/Splunk/6.3.3/SearchReference/Addcoltotals"&gt;http://docs.splunk.com/Documentation/Splunk/6.3.3/SearchReference/Addcoltotals&lt;/A&gt;&lt;BR /&gt;
And if you're not familar with the eval function used in these examples see: &lt;A href="http://docs.splunk.com/Documentation/Splunk/6.3.3/SearchReference/CommonEvalFunctions"&gt;http://docs.splunk.com/Documentation/Splunk/6.3.3/SearchReference/CommonEvalFunctions&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Feb 2016 10:41:08 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/What-is-the-best-way-to-add-a-set-of-values-in-a-table-subtract/m-p/204432#M59381</guid>
      <dc:creator>jplumsdaine22</dc:creator>
      <dc:date>2016-02-16T10:41:08Z</dc:date>
    </item>
    <item>
      <title>Re: What is the best way to add a set of values in a table, subtract another set of values, then append the result back to the table?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/What-is-the-best-way-to-add-a-set-of-values-in-a-table-subtract/m-p/204433#M59382</link>
      <description>&lt;P&gt;I tried this generic option that should work for any number of values.&lt;BR /&gt;
I have tested it with your example and it seems to work fine:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| inputcsv mycsv.csv
| sort Type
| streamstats count as n
| eventstats median(n) as median
| eval ValueSign = if(median &amp;gt; n, Value, -Value)
| addcoltotals
| fillnull value=E
| where n &amp;lt; median
| fields Type, ValueSign
| rename ValueSign as Value
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 16 Feb 2016 10:52:20 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/What-is-the-best-way-to-add-a-set-of-values-in-a-table-subtract/m-p/204433#M59382</guid>
      <dc:creator>javiergn</dc:creator>
      <dc:date>2016-02-16T10:52:20Z</dc:date>
    </item>
    <item>
      <title>Re: What is the best way to add a set of values in a table, subtract another set of values, then append the result back to the table?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/What-is-the-best-way-to-add-a-set-of-values-in-a-table-subtract/m-p/204434#M59383</link>
      <description>&lt;P&gt;Thank you! That was exactly what I was looking for! &lt;/P&gt;</description>
      <pubDate>Tue, 16 Feb 2016 11:04:54 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/What-is-the-best-way-to-add-a-set-of-values-in-a-table-subtract/m-p/204434#M59383</guid>
      <dc:creator>Stevelim</dc:creator>
      <dc:date>2016-02-16T11:04:54Z</dc:date>
    </item>
  </channel>
</rss>

