<?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 conditional stats function in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/conditional-stats-function/m-p/96439#M24921</link>
    <description>&lt;P&gt;Hi Folks,&lt;/P&gt;

&lt;P&gt;I need to use conditional stats&lt;BR /&gt;
e.g current: &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| stats avg(res_time) count(res_time) by transaction
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;required &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| if transaction == tname stats sum(somefield) else count(res_time) by transaction.
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;only one transaction i need sum but count for rest ?&lt;/P&gt;

&lt;P&gt;More Info:&lt;BR /&gt;
Thanks for the prompt response,Not sure if i'm able to explain the issue properly. Here are details. I had tried eval as you had suggested but since every transaction is one event. I have no issue with avg which i'm already calculating in eval.&lt;/P&gt;

&lt;P&gt;my event line is: &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;"2011-11-22 00:00:00 tname res_time trans_count" 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;with value let's say: &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;"2011-11-22 00:00:00 SELECT 5 3" 

|eval res_time = if (transaction =="tname",res_time/trans_count,res_time) 
| stats by avg(res_time),count(res_time)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;but requirement is &lt;CODE&gt;trans_count&lt;/CODE&gt; should be summed up in count for taking volume of transaction and one way was doing is &lt;CODE&gt;sum(trans_count)&lt;/CODE&gt; only for this transaction and simple &lt;CODE&gt;count(res_time)&lt;/CODE&gt; for all others since they have one occurrence (trans_count):&lt;/P&gt;

&lt;P&gt;so that why i need conditional sum for one type of transaction and count for rest.&lt;/P&gt;

&lt;P&gt;Thanks,&lt;BR /&gt;
Amit &lt;/P&gt;</description>
    <pubDate>Sat, 22 Oct 2011 12:22:01 GMT</pubDate>
    <dc:creator>amitsehgal</dc:creator>
    <dc:date>2011-10-22T12:22:01Z</dc:date>
    <item>
      <title>conditional stats function</title>
      <link>https://community.splunk.com/t5/Splunk-Search/conditional-stats-function/m-p/96439#M24921</link>
      <description>&lt;P&gt;Hi Folks,&lt;/P&gt;

&lt;P&gt;I need to use conditional stats&lt;BR /&gt;
e.g current: &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| stats avg(res_time) count(res_time) by transaction
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;required &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| if transaction == tname stats sum(somefield) else count(res_time) by transaction.
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;only one transaction i need sum but count for rest ?&lt;/P&gt;

&lt;P&gt;More Info:&lt;BR /&gt;
Thanks for the prompt response,Not sure if i'm able to explain the issue properly. Here are details. I had tried eval as you had suggested but since every transaction is one event. I have no issue with avg which i'm already calculating in eval.&lt;/P&gt;

&lt;P&gt;my event line is: &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;"2011-11-22 00:00:00 tname res_time trans_count" 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;with value let's say: &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;"2011-11-22 00:00:00 SELECT 5 3" 

|eval res_time = if (transaction =="tname",res_time/trans_count,res_time) 
| stats by avg(res_time),count(res_time)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;but requirement is &lt;CODE&gt;trans_count&lt;/CODE&gt; should be summed up in count for taking volume of transaction and one way was doing is &lt;CODE&gt;sum(trans_count)&lt;/CODE&gt; only for this transaction and simple &lt;CODE&gt;count(res_time)&lt;/CODE&gt; for all others since they have one occurrence (trans_count):&lt;/P&gt;

&lt;P&gt;so that why i need conditional sum for one type of transaction and count for rest.&lt;/P&gt;

&lt;P&gt;Thanks,&lt;BR /&gt;
Amit &lt;/P&gt;</description>
      <pubDate>Sat, 22 Oct 2011 12:22:01 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/conditional-stats-function/m-p/96439#M24921</guid>
      <dc:creator>amitsehgal</dc:creator>
      <dc:date>2011-10-22T12:22:01Z</dc:date>
    </item>
    <item>
      <title>Re: conditional stats function</title>
      <link>https://community.splunk.com/t5/Splunk-Search/conditional-stats-function/m-p/96440#M24922</link>
      <description>&lt;P&gt;This may not do exactly what you're looking for, but it's probably close.  The idea is to use eval to play with some of the values of the fields so that your stats command can work without needing to be conditional.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval tname_value=if(transaction=="tname",somefield,0)
| eval res_time_value=if(transaction!="tname",res_time,0)
| stats sum(tname_value),count(res_time_value) by transaction
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The general idea is that &lt;CODE&gt;eval&lt;/CODE&gt; can do the conditional parts for you where &lt;CODE&gt;stats&lt;/CODE&gt; cannot.  So, using &lt;CODE&gt;eval&lt;/CODE&gt; we make up some new fields to do your &lt;CODE&gt;stats&lt;/CODE&gt; computation on -- and use 0 as a filler value.  &lt;/P&gt;

&lt;HR /&gt;

&lt;P&gt;UPDATE&lt;/P&gt;

&lt;P&gt;Actually, I'm not 100% sure this is going to get you exactly where you want to be.  It dawned on me right after I posted this that 0 as a filler value will still be counted in your &lt;CODE&gt;count(res_time_value)&lt;/CODE&gt;, and could affect averages and so on.  The general plan for using &lt;CODE&gt;eval&lt;/CODE&gt; to do the conditional part seems sound, but needs some more work...&lt;/P&gt;</description>
      <pubDate>Sat, 22 Oct 2011 16:20:49 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/conditional-stats-function/m-p/96440#M24922</guid>
      <dc:creator>dwaddle</dc:creator>
      <dc:date>2011-10-22T16:20:49Z</dc:date>
    </item>
    <item>
      <title>Re: conditional stats function</title>
      <link>https://community.splunk.com/t5/Splunk-Search/conditional-stats-function/m-p/96441#M24923</link>
      <description>&lt;P&gt;you can avoid the &lt;CODE&gt;0&lt;/CODE&gt; filler by just using &lt;CODE&gt;null()&lt;/CODE&gt;. Then neither &lt;CODE&gt;count()&lt;/CODE&gt; nor &lt;CODE&gt;sum()&lt;/CODE&gt; will include the filler.&lt;/P&gt;</description>
      <pubDate>Sat, 22 Oct 2011 23:16:40 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/conditional-stats-function/m-p/96441#M24923</guid>
      <dc:creator>gkanapathy</dc:creator>
      <dc:date>2011-10-22T23:16:40Z</dc:date>
    </item>
    <item>
      <title>Re: conditional stats function</title>
      <link>https://community.splunk.com/t5/Splunk-Search/conditional-stats-function/m-p/96442#M24924</link>
      <description>&lt;P&gt;Just compute all values for all, then select which ones you want. There is no significant performance difference in computing extra sums or averages:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| stats avg(res_time) 
        count(res_time) as countrestime
        sum(somefield) as sumsomefield
  by transaction
| eval mydesiredvalue = if(transaction=="tname",sumsomefield,countrestime)
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 22 Oct 2011 23:16:43 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/conditional-stats-function/m-p/96442#M24924</guid>
      <dc:creator>gkanapathy</dc:creator>
      <dc:date>2011-10-22T23:16:43Z</dc:date>
    </item>
    <item>
      <title>Re: conditional stats function</title>
      <link>https://community.splunk.com/t5/Splunk-Search/conditional-stats-function/m-p/96443#M24925</link>
      <description>&lt;P&gt;Thanks...ihad fixed this issue by having sum(eval(tname exists,tnamecount,else 1)&lt;/P&gt;</description>
      <pubDate>Sun, 23 Oct 2011 00:22:40 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/conditional-stats-function/m-p/96443#M24925</guid>
      <dc:creator>amitsehgal</dc:creator>
      <dc:date>2011-10-23T00:22:40Z</dc:date>
    </item>
    <item>
      <title>Re: conditional stats function</title>
      <link>https://community.splunk.com/t5/Splunk-Search/conditional-stats-function/m-p/96444#M24926</link>
      <description>&lt;P&gt;Use built in sum+eval combos:&lt;/P&gt;

&lt;P&gt;.. | stats count(eval(action=="Denied")) as denies by source_ip &lt;/P&gt;

&lt;P&gt;You can use an eval(field="value") inside of a sum or count inside of a stats command.  Works likea charm.&lt;/P&gt;</description>
      <pubDate>Fri, 10 May 2013 15:25:52 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/conditional-stats-function/m-p/96444#M24926</guid>
      <dc:creator>raziasaduddin</dc:creator>
      <dc:date>2013-05-10T15:25:52Z</dc:date>
    </item>
  </channel>
</rss>

