<?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 combine multiple stats searches and get a result? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-combine-multiple-stats-searches-and-get-a-result/m-p/515198#M144642</link>
    <description>&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;can you post sample data as I suppose that there could be easier solution than those two queries....&lt;/P&gt;&lt;P&gt;r. Ismo&lt;/P&gt;</description>
    <pubDate>Thu, 20 Aug 2020 12:22:28 GMT</pubDate>
    <dc:creator>isoutamo</dc:creator>
    <dc:date>2020-08-20T12:22:28Z</dc:date>
    <item>
      <title>How to combine multiple stats searches and get a result?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-combine-multiple-stats-searches-and-get-a-result/m-p/515084#M144596</link>
      <description>&lt;P&gt;I have two individual stats searches that return a single value each. How can I combine the two to get a ratio?&lt;/P&gt;&lt;P&gt;The index is basically a table of Transaction IDs. There can be multiple entries for an ID. For example&lt;/P&gt;&lt;TABLE border="1" width="100%"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD width="50%" height="25px"&gt;&lt;STRONG&gt;Transaction ID&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD width="50%" height="25px"&gt;&lt;STRONG&gt;Status&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="50%" height="25px"&gt;txn1&lt;/TD&gt;&lt;TD width="50%" height="25px"&gt;200&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="50%" height="25px"&gt;txn1&lt;/TD&gt;&lt;TD width="50%" height="25px"&gt;500&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="50%" height="25px"&gt;txn2&lt;/TD&gt;&lt;TD width="50%" height="25px"&gt;200&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;txn3&lt;/TD&gt;&lt;TD&gt;200&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Search #1 tells me the number of transactions that ended in an error by looking at the last Status of a transaction ID:&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;&lt;FONT face="courier new,courier"&gt;baseSearch | stats latest(status) as lastTxnStatus by txn_id | where lastTxnStatus &amp;gt;= 500 |&amp;nbsp;stats dc(txn_id)&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Search #2 tells me the total number of transactions:&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;&lt;FONT face="courier new,courier"&gt;baseSearch |&amp;nbsp;stats dc(txn_id)&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to get a mathematical result of: &lt;FONT face="courier new,courier"&gt;100 * Search #1 / Search #2.&lt;/FONT&gt; How can I do that? The trouble I'm having is with the "where" command in Search #1 - that complicates everything. Using the data in the table above, the result would 33.3333% (i.e. 100 * 1/3).&lt;/P&gt;</description>
      <pubDate>Thu, 20 Aug 2020 01:13:25 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-combine-multiple-stats-searches-and-get-a-result/m-p/515084#M144596</guid>
      <dc:creator>splunkuserCA1</dc:creator>
      <dc:date>2020-08-20T01:13:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to combine multiple stats searches and get a result?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-combine-multiple-stats-searches-and-get-a-result/m-p/515098#M144598</link>
      <description>&lt;P&gt;Got the answer.&lt;/P&gt;&lt;P&gt;search1, modified to rename the column:&amp;nbsp;&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;&lt;FONT face="courier new,courier"&gt;baseSearch | stats latest(status) as lastTxnStatus by txn_id | where lastTxnStatus &amp;gt;= 500 |&amp;nbsp;stats dc(txn_id) as TotalFailures&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;search2, modified to rename the column:&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;&lt;FONT face="courier new,courier"&gt;baseSearch |&amp;nbsp;stats dc(txn_id) as TotalValues&lt;/FONT&gt;&lt;/P&gt;&lt;P class="lia-indent-padding-left-30px"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Combined:&lt;/P&gt;&lt;P&gt;&lt;EM&gt;search1&lt;/EM&gt;&amp;nbsp;|&amp;nbsp;&amp;nbsp;append [ search &lt;EM&gt;search2&lt;/EM&gt;] | stats values(TotalFailures) as S1, values(TotalValues) as S2 | eval ratio=round(100*S1/S2, 2)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;* Need to use append to combine the searches. But after that, they are in 2 columns over 2 different rows.&lt;/P&gt;&lt;P&gt;* So I need to use "stats" one final time to combine them into a single row with 2 columns.&lt;/P&gt;&lt;P&gt;* Finally, I use eval to get the ratio.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was hoping there might be a simpler way to do this!&lt;/P&gt;</description>
      <pubDate>Thu, 20 Aug 2020 02:14:47 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-combine-multiple-stats-searches-and-get-a-result/m-p/515098#M144598</guid>
      <dc:creator>splunkuserCA1</dc:creator>
      <dc:date>2020-08-20T02:14:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to combine multiple stats searches and get a result?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-combine-multiple-stats-searches-and-get-a-result/m-p/515198#M144642</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;can you post sample data as I suppose that there could be easier solution than those two queries....&lt;/P&gt;&lt;P&gt;r. Ismo&lt;/P&gt;</description>
      <pubDate>Thu, 20 Aug 2020 12:22:28 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-combine-multiple-stats-searches-and-get-a-result/m-p/515198#M144642</guid>
      <dc:creator>isoutamo</dc:creator>
      <dc:date>2020-08-20T12:22:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to combine multiple stats searches and get a result?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-combine-multiple-stats-searches-and-get-a-result/m-p/515242#M144651</link>
      <description>&lt;P&gt;While I can't post the exact data, this table should contain enough. As you see, there are multiple entries for some transactions (like txn1), while other transactions have just 1 entry:&lt;/P&gt;&lt;TABLE border="1" width="100%"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD width="25%" height="25px"&gt;&lt;STRONG&gt;Date&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;&lt;STRONG&gt;Transaction ID&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD width="50%" height="25px"&gt;&lt;STRONG&gt;Status&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="25%" height="25px"&gt;2020-07-01 08:00:00&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;txn1&lt;/TD&gt;&lt;TD width="50%" height="25px"&gt;200&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="25%" height="25px"&gt;2020-07-01 08:20:00&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;txn1&lt;/TD&gt;&lt;TD width="50%" height="25px"&gt;500&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="25%" height="25px"&gt;2020-07-01 09:00:00&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;txn2&lt;/TD&gt;&lt;TD width="50%" height="25px"&gt;200&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD width="25%" height="25px"&gt;2020-07-01 10:00:00&lt;/TD&gt;&lt;TD width="25%" height="25px"&gt;txn3&lt;/TD&gt;&lt;TD width="50%" height="25px"&gt;200&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Thu, 20 Aug 2020 15:03:53 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-combine-multiple-stats-searches-and-get-a-result/m-p/515242#M144651</guid>
      <dc:creator>splunkuserCA1</dc:creator>
      <dc:date>2020-08-20T15:03:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to combine multiple stats searches and get a result?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-combine-multiple-stats-searches-and-get-a-result/m-p/515663#M144789</link>
      <description>&lt;LI-CODE lang="markup"&gt;baseSearch | stats latest(status) as lastTxnStatus by txn_id | eval error=if(lastTxnStatus &amp;gt;= 500, 1, 0) | stats sum(error) as errors, dc(txn_id) as TotalTransactions | eval percentageFail = (100 * errors) / TotalTransactions&lt;/LI-CODE&gt;</description>
      <pubDate>Sun, 23 Aug 2020 22:53:43 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-combine-multiple-stats-searches-and-get-a-result/m-p/515663#M144789</guid>
      <dc:creator>ITWhisperer</dc:creator>
      <dc:date>2020-08-23T22:53:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to combine multiple stats searches and get a result?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-combine-multiple-stats-searches-and-get-a-result/m-p/515669#M144791</link>
      <description>&lt;P&gt;Simplicity is derived from reducing the two searches to a single searches. There are often several ways to get the same result in Splunk - some more performant than others - which is useful in large data sets.&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Here's a variant that uses eventstats to get the unique count of tx ids which before the where clause. It then uses values() to pass that total through to the final eval - it could also be done with 'by TotalTxIds' as there's only one value.&amp;nbsp; As you have already split by txn_id, then count will give you the effective result of dc()&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;baseSearch 
| stats latest(status) as lastTxnStatus by txn_id 
| eventstats count as TotalTxIds
| where lastTxnStatus&amp;gt;=500
| stats values(TotalTxIds) as TotalTxIds count as TxEndingInError
| eval failureRate = round(TxEndingInError / TotalTxIds * 100)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Aug 2020 00:45:39 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-combine-multiple-stats-searches-and-get-a-result/m-p/515669#M144791</guid>
      <dc:creator>bowesmana</dc:creator>
      <dc:date>2020-08-24T00:45:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to combine multiple stats searches and get a result?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-combine-multiple-stats-searches-and-get-a-result/m-p/515842#M144840</link>
      <description>&lt;P&gt;This is a very clever use of stats values()!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can see that both responses are functionally equivalent and are way better than my original query. I've accepted the first reply. For the record, I also found another efficient way of expressing the query using "top":&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;basesearch | stats latest(status) as lastStatus by lastTxnStatus
| top lastStatus limit=0
| search lastStatus &amp;gt;= 500
| stats sum(percent) as BadPercentage&lt;/LI-CODE&gt;</description>
      <pubDate>Mon, 24 Aug 2020 16:54:42 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-combine-multiple-stats-searches-and-get-a-result/m-p/515842#M144840</guid>
      <dc:creator>splunkuserCA1</dc:creator>
      <dc:date>2020-08-24T16:54:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to combine multiple stats searches and get a result?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-combine-multiple-stats-searches-and-get-a-result/m-p/516158#M145024</link>
      <description>&lt;P&gt;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/224590"&gt;@splunkuserCA1&lt;/a&gt;&amp;nbsp; Haha, yes, you've discovered the beauty of Splunk, in that there is always more than one way of doing a task. At some point in your Splunk journey, you may well start to think about which one performs better than the other and that you can get by looking at the job inspector.&lt;/P&gt;&lt;P&gt;There are definitely performance differences between different techniques and if you have large data sets, you'll start to hit Splunk limits with some techniques.&lt;/P&gt;&lt;P&gt;Happy Splunking!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Aug 2020 22:07:59 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-combine-multiple-stats-searches-and-get-a-result/m-p/516158#M145024</guid>
      <dc:creator>bowesmana</dc:creator>
      <dc:date>2020-08-25T22:07:59Z</dc:date>
    </item>
  </channel>
</rss>

