<?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: Sum field value by different fields and merge together in 1 table in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Sum-field-value-by-different-fields-and-merge-together-in-1/m-p/292238#M175649</link>
    <description>&lt;P&gt;Hi! Try to use this search: &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;|stats sum(amount) as total_debit by dr_account | rename  dr_account  AS account 
|append [stats sum(amount) as total_credit by cr_account | rename  cr_account  AS account] 
|stats value(total_debit) AS total_debit, value(total_credit) AS total_credit by account | eval balance=total_debit-total_credit 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Also you can use &lt;CODE&gt;appendpipe&lt;/CODE&gt; command for search optimization like this: &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;|rename  dr_account  AS account | stats sum(amount) as total_debit by account 
|appendpipe [stats sum(amount) as total_credit by cr_account | rename  cr_account  AS account] 
|stats value(total_debit) AS total_debit, value(total_credit) AS total_credit by account |eval balance=total_debit-total_credit 
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 09 Feb 2017 11:03:34 GMT</pubDate>
    <dc:creator>ronekarleone</dc:creator>
    <dc:date>2017-02-09T11:03:34Z</dc:date>
    <item>
      <title>Sum field value by different fields and merge together in 1 table</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Sum-field-value-by-different-fields-and-merge-together-in-1/m-p/292237#M175648</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;I have a list of events here from an account management system.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;"_time",dr_account,cr_account,amount
"2017-02-09T17:20:19.000+0700",111111,222222,1500000
"2017-02-09T17:20:19.000+0700",111111,333333,1000000
"2017-02-09T17:20:19.000+0700",222222,555555,11070000
"2017-02-09T17:20:19.000+0700",333333,111111,3000000
"2017-02-09T17:20:19.000+0700",444444,111111,1250000
"2017-02-09T17:20:19.000+0700",111111,555555,3000000
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I am going to make a aggregation report by &lt;CODE&gt;sum the amount&lt;/CODE&gt; field by both &lt;CODE&gt;dr_account&lt;/CODE&gt; and &lt;CODE&gt;cr_account&lt;/CODE&gt; and, then display the &lt;CODE&gt;total debit, total credit and balance (=total debit - total credit) on a table&lt;/CODE&gt; for each account, for example:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;account,total_debit,total_credit,balance
111111,5500000,4250000,1250000
222222,11070000,1500000,9570000
...
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I am trying to use &lt;CODE&gt;stats sum(amount) as total_debit by dr_account | appendcols [stats sum(amount) as total_credit by cr_account]&lt;/CODE&gt; but no luck.&lt;/P&gt;

&lt;P&gt;Could you guys please help me on this case ?&lt;/P&gt;

&lt;P&gt;Thank you very much.&lt;BR /&gt;
Lang&lt;/P&gt;</description>
      <pubDate>Thu, 09 Feb 2017 10:35:27 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Sum-field-value-by-different-fields-and-merge-together-in-1/m-p/292237#M175648</guid>
      <dc:creator>langlv</dc:creator>
      <dc:date>2017-02-09T10:35:27Z</dc:date>
    </item>
    <item>
      <title>Re: Sum field value by different fields and merge together in 1 table</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Sum-field-value-by-different-fields-and-merge-together-in-1/m-p/292238#M175649</link>
      <description>&lt;P&gt;Hi! Try to use this search: &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;|stats sum(amount) as total_debit by dr_account | rename  dr_account  AS account 
|append [stats sum(amount) as total_credit by cr_account | rename  cr_account  AS account] 
|stats value(total_debit) AS total_debit, value(total_credit) AS total_credit by account | eval balance=total_debit-total_credit 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Also you can use &lt;CODE&gt;appendpipe&lt;/CODE&gt; command for search optimization like this: &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;|rename  dr_account  AS account | stats sum(amount) as total_debit by account 
|appendpipe [stats sum(amount) as total_credit by cr_account | rename  cr_account  AS account] 
|stats value(total_debit) AS total_debit, value(total_credit) AS total_credit by account |eval balance=total_debit-total_credit 
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 09 Feb 2017 11:03:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Sum-field-value-by-different-fields-and-merge-together-in-1/m-p/292238#M175649</guid>
      <dc:creator>ronekarleone</dc:creator>
      <dc:date>2017-02-09T11:03:34Z</dc:date>
    </item>
    <item>
      <title>Re: Sum field value by different fields and merge together in 1 table</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Sum-field-value-by-different-fields-and-merge-together-in-1/m-p/292239#M175650</link>
      <description>&lt;P&gt;Try this one:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; |appendpipe [stats sum(amount) as total_debit by dr_account |rename dr_account as account]
 |appendpipe [stats sum(amount) as total_credit by cr_account |rename cr_account as account]
 |stats values(total*) as total* by account
 |eval balance=total_debit-total_credit
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Result:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;account total_credit    total_debit balance
--------------------------------------------------
111111  4250000       5500000       1250000
222222  1500000       11070000      9570000
333333  1000000       3000000       2000000
444444                    1250000    
555555  14070000         
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 09 Feb 2017 12:00:12 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Sum-field-value-by-different-fields-and-merge-together-in-1/m-p/292239#M175650</guid>
      <dc:creator>ehudb</dc:creator>
      <dc:date>2017-02-09T12:00:12Z</dc:date>
    </item>
    <item>
      <title>Re: Sum field value by different fields and merge together in 1 table</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Sum-field-value-by-different-fields-and-merge-together-in-1/m-p/292240#M175651</link>
      <description>&lt;P&gt;Try like this (everything before &lt;CODE&gt;| eval temp&lt;/CODE&gt; is just to generate sample data, replace it with your search)&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| gentimes start=-1 | eval host="2017-02-09T17:20:19.000+0700,111111,222222,1500000 2017-02-09T17:20:19.000+0700,111111,333333,1000000 2017-02-09T17:20:19.000+0700,222222,555555,11070000 2017-02-09T17:20:19.000+0700,333333,111111,3000000 2017-02-09T17:20:19.000+0700,444444,111111,1250000 2017-02-09T17:20:19.000+0700,111111,555555,3000000"| table host | makemv host | mvexpand host | rex field=host "^(?&amp;lt;time&amp;gt;[^,]+),(?&amp;lt;dr_account&amp;gt;[^,]+),(?&amp;lt;cr_account&amp;gt;[^,]+),(?&amp;lt;amount&amp;gt;[^,]+)"  | eval _time=strptime(time,"%Y-%m-%dT%H:%M:%S.%N%z")| fields - host time 
| eval temp=dr_account.":debit:".amount."##".cr_account.":credit:".amount | table temp | makemv temp delim="##" | mvexpand temp | rex field=temp "(?&amp;lt;account&amp;gt;[^\:]+)\:(?&amp;lt;action&amp;gt;[^\:]+)\:(?&amp;lt;amount&amp;gt;.+)" | fields - temp | chart sum(amount) over account by action | fillnull value=0| eval balance=debit-credit
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 09 Feb 2017 15:59:12 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Sum-field-value-by-different-fields-and-merge-together-in-1/m-p/292240#M175651</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2017-02-09T15:59:12Z</dc:date>
    </item>
    <item>
      <title>Re: Sum field value by different fields and merge together in 1 table</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Sum-field-value-by-different-fields-and-merge-together-in-1/m-p/292241#M175652</link>
      <description>&lt;P&gt;Extra credit for showing the sample data.  Just missed a perfect score by the field order and name.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| table account debit credit balance 
| rename debit as total_debit credit as total_credit
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 09 Feb 2017 16:33:30 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Sum-field-value-by-different-fields-and-merge-together-in-1/m-p/292241#M175652</guid>
      <dc:creator>DalJeanis</dc:creator>
      <dc:date>2017-02-09T16:33:30Z</dc:date>
    </item>
    <item>
      <title>Re: Sum field value by different fields and merge together in 1 table</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Sum-field-value-by-different-fields-and-merge-together-in-1/m-p/292242#M175653</link>
      <description>&lt;P&gt;Works fine.  Maybe a little more readable than somesoni2's.&lt;/P&gt;</description>
      <pubDate>Thu, 09 Feb 2017 16:38:08 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Sum-field-value-by-different-fields-and-merge-together-in-1/m-p/292242#M175653</guid>
      <dc:creator>DalJeanis</dc:creator>
      <dc:date>2017-02-09T16:38:08Z</dc:date>
    </item>
    <item>
      <title>Re: Sum field value by different fields and merge together in 1 table</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Sum-field-value-by-different-fields-and-merge-together-in-1/m-p/292243#M175654</link>
      <description>&lt;P&gt;It works like a charm, add some &lt;CODE&gt;isnull()&lt;/CODE&gt; check and i can now have balance for all accounts.&lt;/P&gt;

&lt;P&gt;Thanks ehudb.&lt;/P&gt;</description>
      <pubDate>Fri, 10 Feb 2017 03:05:50 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Sum-field-value-by-different-fields-and-merge-together-in-1/m-p/292243#M175654</guid>
      <dc:creator>langlv</dc:creator>
      <dc:date>2017-02-10T03:05:50Z</dc:date>
    </item>
  </channel>
</rss>

