<?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 the difference in field values in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-difference-in-field-values/m-p/203935#M59251</link>
    <description>&lt;P&gt;Try the following:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; (index="first" sourcetype="anysourcetype") OR (index="second" sourcetype="otherstr") |  stats count as count1 by FIELD1 | appendcols [search index="second" sourcetype="otherstr"| stats count as count2 by FIELD1]|  eval Difference=count1-count2
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 03 Nov 2016 12:52:16 GMT</pubDate>
    <dc:creator>niketn</dc:creator>
    <dc:date>2016-11-03T12:52:16Z</dc:date>
    <item>
      <title>How to calculate the difference in field values</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-difference-in-field-values/m-p/203930#M59246</link>
      <description>&lt;P&gt;Hi all.&lt;/P&gt;

&lt;P&gt;I have two basic searches like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=first sourcetype=first-sourcetype | stats count by FIELD1

index=second sourcetype=second-sourcetype | stats count by FIELD1
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;CODE&gt;FIELD1&lt;/CODE&gt; has values like ONE, TWO, THREE.&lt;/P&gt;

&lt;P&gt;I want to calculate the difference between the two searches value per value. How i can do? The two sourcetypes are equal, the difference is the quantity of values on each one.&lt;/P&gt;

&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 02 Nov 2016 22:31:57 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-difference-in-field-values/m-p/203930#M59246</guid>
      <dc:creator>changux</dc:creator>
      <dc:date>2016-11-02T22:31:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate the difference in field values</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-difference-in-field-values/m-p/203931#M59247</link>
      <description>&lt;P&gt;Try this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;(index=first OR index=second) (sourcetype=first-sourcetype OR sourcetype=second-sourcetype) |
chart count over FIELD1 by index |
eval diff=first-second
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 02 Nov 2016 22:43:54 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-difference-in-field-values/m-p/203931#M59247</guid>
      <dc:creator>twinspop</dc:creator>
      <dc:date>2016-11-02T22:43:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate the difference in field values</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-difference-in-field-values/m-p/203932#M59248</link>
      <description>&lt;P&gt;Works great. if my searches are:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=first sourcetype=anysourcetype | join ID [search index=second sourcetype=othersrt]
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;and&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=second sourcetype=othersrt
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;How i can obtain the same calculation?&lt;/P&gt;

&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Wed, 02 Nov 2016 23:37:20 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-difference-in-field-values/m-p/203932#M59248</guid>
      <dc:creator>changux</dc:creator>
      <dc:date>2016-11-02T23:37:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate the difference in field values</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-difference-in-field-values/m-p/203933#M59249</link>
      <description>&lt;P&gt;&lt;STRONG&gt;Option 1&lt;/STRONG&gt;&lt;/P&gt;

&lt;P&gt;Step 1: Run search index="first" sourcetype="anysourcetype" and save as Event Type "firstany".&lt;/P&gt;

&lt;P&gt;Step 2: Run search index="second" sourcetype="othersrt" and save as Event Type "secondother"&lt;/P&gt;

&lt;P&gt;Step 3: Run the following search query&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;(eventtype="firstany" ) OR (eventtype="secondother") | chart count over FIELD1 by eventtype | eval difference=firstany-secondother
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Also refer to previous answer on similar lines as this seems to be a duplicate thread.&lt;BR /&gt;
&lt;A href="https://answers.splunk.com/answers/470876/how-to-calculate-the-difference-between-count-of-t.html#answer-469923"&gt;https://answers.splunk.com/answers/470876/how-to-calculate-the-difference-between-count-of-t.html#answer-469923&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;Option 2&lt;/STRONG&gt;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;(index="first" sourcetype="anysourcetype") OR (index="second" sourcetype="otherstr") | eval statsfield= FIELD1 + " - " +  index  + " - " + sourcetype | stats count by statsfield | delta count as Difference
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Please let me know if this is not what you are looking for, so that I may assist.&lt;/P&gt;</description>
      <pubDate>Thu, 03 Nov 2016 06:47:24 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-difference-in-field-values/m-p/203933#M59249</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2016-11-03T06:47:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate the difference in field values</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-difference-in-field-values/m-p/203934#M59250</link>
      <description>&lt;P&gt;Thanks!&lt;BR /&gt;
If my searches are:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; index=first sourcetype=anysourcetype | join ID [search index=second sourcetype=othersrt]

 index=second sourcetype=othersrt
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Any suggestion?&lt;/P&gt;</description>
      <pubDate>Thu, 03 Nov 2016 12:17:00 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-difference-in-field-values/m-p/203934#M59250</guid>
      <dc:creator>changux</dc:creator>
      <dc:date>2016-11-03T12:17:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate the difference in field values</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-difference-in-field-values/m-p/203935#M59251</link>
      <description>&lt;P&gt;Try the following:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; (index="first" sourcetype="anysourcetype") OR (index="second" sourcetype="otherstr") |  stats count as count1 by FIELD1 | appendcols [search index="second" sourcetype="otherstr"| stats count as count2 by FIELD1]|  eval Difference=count1-count2
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 03 Nov 2016 12:52:16 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-difference-in-field-values/m-p/203935#M59251</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2016-11-03T12:52:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate the difference in field values</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-difference-in-field-values/m-p/203936#M59252</link>
      <description>&lt;P&gt;It's very tempting to solve this problem with a second join.   However breaking down the requirements with paper and pencil,  there are other ways to do it in more of a splunklike fashion. (and this means with far better performance, without breaking map-reduce, and with no limits on the size of your results.)&lt;/P&gt;

&lt;P&gt;The first search (assuming there's an implicit &lt;CODE&gt;| stats count by FIELD1&lt;/CODE&gt; on the end as before)  is saying "find me the counts for all values of FIELD1, for just the rows in &lt;CODE&gt;index=first sourcetype=anysourcetype&lt;/CODE&gt; whose ID appears also in &lt;CODE&gt;index=second sourcetype=othersrt&lt;/CODE&gt;. &lt;/P&gt;

&lt;P&gt;Now to compare the "search1" counts vs the "search2" couonts, we kind of need to transform the incoming rows in two different (kind of mutually exclusive) ways.   Obviously we can't transform the results in incompatible ways, and this means we have to do the same work without transforming the incoming rows at all. Whenever you hit this sort of thing you should think of &lt;CODE&gt;eventstats&lt;/CODE&gt; and &lt;CODE&gt;streamstats&lt;/CODE&gt; because they are your tools for that. &lt;/P&gt;

&lt;P&gt;So, a way to get search1's,  (and I've gone ahead and put back the &lt;CODE&gt;chart count by ID&lt;/CODE&gt; bit)&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; (index=first OR index=second) (sourcetype=first-sourcetype OR sourcetype=second-sourcetype)
| eventstats values(index) as indexesWithThisID by ID
| search indexesWithThisID=second index=first
| chart count by ID
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;it's kind of weird.  We send eventstats off on a mission to find, for each value of "ID", which indexes that value in across the whole set. We then paint those indexes on each row as a multivalue field called &lt;CODE&gt;indexesWithThisID&lt;/CODE&gt;. &lt;/P&gt;

&lt;P&gt;The end result is that we can filter the set down just with &lt;CODE&gt;| search indexesWithThisID=second index=first&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;Let's now modify this, so that it can take the FIELD1 counts for these events, and compare them with the FIELD1 counts for the &lt;CODE&gt;index=second  sourcetype=othersrt&lt;/CODE&gt; events.   Here I'm using a conditional eval to break it into steps. &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; (index=first OR index=second) (sourcetype=first-sourcetype OR sourcetype=second-sourcetype)
| eventstats values(index) as indexesWithThisID by ID
| eval matchesWhichSearch=case(indexesWithThisID="second" AND index="first",1,index="second",2,true(),-1)
| search matchesWhichSearch&amp;gt;0
| chart count over FIELD1 by matchesWhichSearch
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Note - it may make sense for this to be moved out to its own question but I'll leave it here for now. &lt;/P&gt;</description>
      <pubDate>Thu, 03 Nov 2016 17:34:49 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-difference-in-field-values/m-p/203936#M59252</guid>
      <dc:creator>sideview</dc:creator>
      <dc:date>2016-11-03T17:34:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate the difference in field values</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-difference-in-field-values/m-p/203937#M59253</link>
      <description>&lt;P&gt;Thanks @sideview, @niketnilay . I need the &lt;CODE&gt;join&lt;/CODE&gt;, some context to understand:&lt;/P&gt;

&lt;P&gt;Each sourcetype has:&lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;index=first sourcetype=anysourcetype&lt;/CODE&gt; =&amp;gt; Executed tasks&lt;BR /&gt;
&lt;CODE&gt;index=second sourcetype=othersrc&lt;/CODE&gt; =&amp;gt; To execute tasks.&lt;/P&gt;

&lt;P&gt;Then, the join returns to me the REAL executed tasks with some details about it. The field &lt;CODE&gt;FIELD1&lt;/CODE&gt; is originally present at  &lt;CODE&gt;index=second sourcetype=othersrc&lt;/CODE&gt;, so after join, i have the list of REAL executed and the value with states like type1, type2, type3, etc (included in &lt;CODE&gt;FIELD1&lt;/CODE&gt;). I need to show the list of NOT EXECUTED tasks classified by &lt;CODE&gt;FIELD1&lt;/CODE&gt;.&lt;/P&gt;

&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Fri, 04 Nov 2016 00:37:52 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-difference-in-field-values/m-p/203937#M59253</guid>
      <dc:creator>changux</dc:creator>
      <dc:date>2016-11-04T00:37:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate the difference in field values</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-difference-in-field-values/m-p/203938#M59254</link>
      <description>&lt;P&gt;Anybody? Please help me.&lt;/P&gt;</description>
      <pubDate>Mon, 07 Nov 2016 00:29:57 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-difference-in-field-values/m-p/203938#M59254</guid>
      <dc:creator>changux</dc:creator>
      <dc:date>2016-11-07T00:29:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate the difference in field values</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-difference-in-field-values/m-p/203939#M59255</link>
      <description>&lt;P&gt;You can just replace appencols in above example with join ID&lt;/P&gt;

&lt;P&gt;So try the following:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;(index="first" sourcetype="anysourcetype") OR (index="second" sourcetype="otherstr") |  stats count as count1 by FIELD1 | join ID [search index="second" sourcetype="otherstr"| stats count as count2 by FIELD1]|  eval Difference=count1-count2
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;FYI - append, appendcols, join, stats, eventstats, transaction, subsearch etc are all for event correlation and most of them might fit in to return the results you want. However, you should try job inspector to evaluate which one performs best. Refer to following Splunk documentation on how to choose between various event correlation commands&lt;/P&gt;

&lt;P&gt;&lt;A href="http://docs.splunk.com/Documentation/Splunk/latest/Search/Abouteventcorrelation"&gt;http://docs.splunk.com/Documentation/Splunk/latest/Search/Abouteventcorrelation&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Mar 2017 21:08:45 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-calculate-the-difference-in-field-values/m-p/203939#M59255</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2017-03-03T21:08:45Z</dc:date>
    </item>
  </channel>
</rss>

