<?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: IF statement inside EVAL in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/IF-statement-inside-EVAL/m-p/416504#M119866</link>
    <description>&lt;P&gt;You don't have a sourcetype specified in your &lt;CODE&gt;count_total&lt;/CODE&gt; eval.. &lt;/P&gt;</description>
    <pubDate>Tue, 21 Aug 2018 19:04:10 GMT</pubDate>
    <dc:creator>skoelpin</dc:creator>
    <dc:date>2018-08-21T19:04:10Z</dc:date>
    <item>
      <title>IF statement inside EVAL</title>
      <link>https://community.splunk.com/t5/Splunk-Search/IF-statement-inside-EVAL/m-p/416499#M119861</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;

&lt;P&gt;I want to divide AverageCount by AverageTotal. The problem is that Average count is separated by Sourcetype and Average Total is separated by a Field. For example:&lt;/P&gt;

&lt;P&gt;index=x Sourcetype: SAT --&amp;gt; I calculate Average Count using this search &lt;BR /&gt;
index=x Sourcetype:TotalTru Site:SAT --&amp;gt; I calculate Average Total by day using this search&lt;/P&gt;

&lt;P&gt;Is there a way that I can use an eval statement by specifying with an if statement what site to relate the average to. I was thinking:&lt;/P&gt;

&lt;P&gt;If sourcetype: SAT, then eval by site when site: SAT&lt;/P&gt;

&lt;P&gt;index="x" &lt;BR /&gt;
| bin _time span=1d&lt;BR /&gt;
| stats count(Number) as CountEvents by _time, sourcetype &lt;BR /&gt;
| chart avg(CountEvents) as AverageCount by sourcetype&lt;BR /&gt;
| append&lt;BR /&gt;
    [search sourcetype=TotalTru &lt;BR /&gt;
    | eval Total=if(sourcetype==SAT,&lt;BR /&gt;
....&lt;/P&gt;

&lt;P&gt;Thanks!! &lt;/P&gt;</description>
      <pubDate>Mon, 20 Aug 2018 20:49:06 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/IF-statement-inside-EVAL/m-p/416499#M119861</guid>
      <dc:creator>tonahoyos</dc:creator>
      <dc:date>2018-08-20T20:49:06Z</dc:date>
    </item>
    <item>
      <title>Re: IF statement inside EVAL</title>
      <link>https://community.splunk.com/t5/Splunk-Search/IF-statement-inside-EVAL/m-p/416500#M119862</link>
      <description>&lt;P&gt;Yeah, use single quotes to capture a fields value like this &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index="x" 
| bin _time span=1d
| stats eval(if(sourcetype="SAT",'CountEvents',"null")) AS count_avg eval(if(sourcetype="&amp;lt;SOURCETYPE&amp;gt;",'AverageCount',"null")) AS count_total
| eval Total=count_avg/count_total
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Or use &lt;CODE&gt;eventstats&lt;/CODE&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Aug 2018 21:48:27 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/IF-statement-inside-EVAL/m-p/416500#M119862</guid>
      <dc:creator>skoelpin</dc:creator>
      <dc:date>2018-08-20T21:48:27Z</dc:date>
    </item>
    <item>
      <title>Re: IF statement inside EVAL</title>
      <link>https://community.splunk.com/t5/Splunk-Search/IF-statement-inside-EVAL/m-p/416501#M119863</link>
      <description>&lt;P&gt;Hello, &lt;/P&gt;

&lt;P&gt;Thank you for your answer. Should I still use the subsearch? and enter the subsearch with your answer? &lt;/P&gt;

&lt;P&gt;Best. &lt;/P&gt;</description>
      <pubDate>Tue, 21 Aug 2018 18:38:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/IF-statement-inside-EVAL/m-p/416501#M119863</guid>
      <dc:creator>tonahoyos</dc:creator>
      <dc:date>2018-08-21T18:38:13Z</dc:date>
    </item>
    <item>
      <title>Re: IF statement inside EVAL</title>
      <link>https://community.splunk.com/t5/Splunk-Search/IF-statement-inside-EVAL/m-p/416502#M119864</link>
      <description>&lt;P&gt;No subsearch needed. Just use what I provided you &lt;/P&gt;

&lt;P&gt;Also don't forget to accept/upvote if this answered your question&lt;/P&gt;</description>
      <pubDate>Tue, 21 Aug 2018 18:40:39 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/IF-statement-inside-EVAL/m-p/416502#M119864</guid>
      <dc:creator>skoelpin</dc:creator>
      <dc:date>2018-08-21T18:40:39Z</dc:date>
    </item>
    <item>
      <title>Re: IF statement inside EVAL</title>
      <link>https://community.splunk.com/t5/Splunk-Search/IF-statement-inside-EVAL/m-p/416503#M119865</link>
      <description>&lt;P&gt;I am using what you have provided and it states that the eval argument is invalid. &lt;/P&gt;

&lt;P&gt;| stats count(Number) as CountEvents, avg(CountEvents) as AverageCount by date_mday, sourcetype&lt;BR /&gt;
| bin _time span=1d&lt;BR /&gt;
| stats eval(if(sourcetype="SAT",'CountEvents',"null")) as count_avg, eval(if(sourcetype="",'AverageCount',"null")) as count_total&lt;BR /&gt;
| eval Total=count_avg/count_total&lt;/P&gt;

&lt;P&gt;I have tried separating it, but it comes up as invalid also.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 21:00:37 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/IF-statement-inside-EVAL/m-p/416503#M119865</guid>
      <dc:creator>tonahoyos</dc:creator>
      <dc:date>2020-09-29T21:00:37Z</dc:date>
    </item>
    <item>
      <title>Re: IF statement inside EVAL</title>
      <link>https://community.splunk.com/t5/Splunk-Search/IF-statement-inside-EVAL/m-p/416504#M119866</link>
      <description>&lt;P&gt;You don't have a sourcetype specified in your &lt;CODE&gt;count_total&lt;/CODE&gt; eval.. &lt;/P&gt;</description>
      <pubDate>Tue, 21 Aug 2018 19:04:10 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/IF-statement-inside-EVAL/m-p/416504#M119866</guid>
      <dc:creator>skoelpin</dc:creator>
      <dc:date>2018-08-21T19:04:10Z</dc:date>
    </item>
    <item>
      <title>Re: IF statement inside EVAL</title>
      <link>https://community.splunk.com/t5/Splunk-Search/IF-statement-inside-EVAL/m-p/416505#M119867</link>
      <description>&lt;P&gt;@tonahoyos did this solve your question?&lt;/P&gt;</description>
      <pubDate>Wed, 22 Aug 2018 14:06:14 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/IF-statement-inside-EVAL/m-p/416505#M119867</guid>
      <dc:creator>skoelpin</dc:creator>
      <dc:date>2018-08-22T14:06:14Z</dc:date>
    </item>
    <item>
      <title>Re: IF statement inside EVAL</title>
      <link>https://community.splunk.com/t5/Splunk-Search/IF-statement-inside-EVAL/m-p/416506#M119868</link>
      <description>&lt;P&gt;Hello @skoelpin,&lt;/P&gt;

&lt;P&gt;I have not been able to solve the question. I keep trying to work around it and see if there is something missing, but it has not worked out yet.&lt;/P&gt;

&lt;P&gt;Thank you for your time. &lt;/P&gt;</description>
      <pubDate>Thu, 23 Aug 2018 18:55:01 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/IF-statement-inside-EVAL/m-p/416506#M119868</guid>
      <dc:creator>tonahoyos</dc:creator>
      <dc:date>2018-08-23T18:55:01Z</dc:date>
    </item>
    <item>
      <title>Re: IF statement inside EVAL</title>
      <link>https://community.splunk.com/t5/Splunk-Search/IF-statement-inside-EVAL/m-p/416507#M119869</link>
      <description>&lt;P&gt;Did you put the sourcetype in? This will solve the problem.. &lt;/P&gt;

&lt;P&gt;What are the 2 sourcetypes? I will update the query with your sourcetypes&lt;/P&gt;</description>
      <pubDate>Thu, 23 Aug 2018 19:22:49 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/IF-statement-inside-EVAL/m-p/416507#M119869</guid>
      <dc:creator>skoelpin</dc:creator>
      <dc:date>2018-08-23T19:22:49Z</dc:date>
    </item>
    <item>
      <title>Re: IF statement inside EVAL</title>
      <link>https://community.splunk.com/t5/Splunk-Search/IF-statement-inside-EVAL/m-p/416508#M119870</link>
      <description>&lt;P&gt;This is the following Query I am using and it is not working: &lt;/P&gt;

&lt;P&gt;index="jamlog" &lt;BR /&gt;
| timechart count(Number) as CountEvents by sourcetype&lt;BR /&gt;
| appendcols&lt;BR /&gt;
    [search sourcetype="totaltru"&lt;BR /&gt;
| timechart sum(Total) as Total by Site ]&lt;BR /&gt;
| eval Ratio=CountEvents/Total&lt;BR /&gt;
| stats avg(Ratio) by sourcetype&lt;/P&gt;

&lt;P&gt;I want to be able to use these two results and divide them to make a ratio: &lt;/P&gt;

&lt;P&gt;Site        Total&lt;BR /&gt;
DFW       353233&lt;BR /&gt;
SAT        491025&lt;/P&gt;

&lt;P&gt;sourcetype     CountEvents&lt;BR /&gt;
BUF                    2983&lt;BR /&gt;
DFW                   5318&lt;BR /&gt;
HNL                    3730&lt;BR /&gt;
ORD                   7446&lt;BR /&gt;
SAT                    9213&lt;/P&gt;

&lt;P&gt;What do you think I am doing wrong?&lt;/P&gt;</description>
      <pubDate>Thu, 23 Aug 2018 19:55:20 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/IF-statement-inside-EVAL/m-p/416508#M119870</guid>
      <dc:creator>tonahoyos</dc:creator>
      <dc:date>2018-08-23T19:55:20Z</dc:date>
    </item>
    <item>
      <title>Re: IF statement inside EVAL</title>
      <link>https://community.splunk.com/t5/Splunk-Search/IF-statement-inside-EVAL/m-p/416509#M119871</link>
      <description>&lt;P&gt;Why do you keep using a subsearch after I said you don't need it? You also didn't give me both sourcetypes... &lt;/P&gt;

&lt;P&gt;Once again, the below query WILL WORK if you simply follow my advice... What are your two sourcetypes? Is it &lt;CODE&gt;totaltru&lt;/CODE&gt; and &lt;CODE&gt;SAT&lt;/CODE&gt;? If so then the query below WILL WORK&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; index="jamlog" 
 | bin _time span=1d
 | stats eval(if(sourcetype="SAT",'CountEvents',0)) AS count_avg eval(if(sourcetype="totaltru",'AverageCount',0)) AS count_total by _time
 | eval Total=count_avg/count_total
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 23 Aug 2018 20:36:21 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/IF-statement-inside-EVAL/m-p/416509#M119871</guid>
      <dc:creator>skoelpin</dc:creator>
      <dc:date>2018-08-23T20:36:21Z</dc:date>
    </item>
    <item>
      <title>Re: IF statement inside EVAL</title>
      <link>https://community.splunk.com/t5/Splunk-Search/IF-statement-inside-EVAL/m-p/416510#M119872</link>
      <description>&lt;P&gt;Yes, those are the sourcetypes. Thank you for your help, but the query did not work, that is why I am using the subsearch. &lt;/P&gt;</description>
      <pubDate>Thu, 23 Aug 2018 20:49:12 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/IF-statement-inside-EVAL/m-p/416510#M119872</guid>
      <dc:creator>tonahoyos</dc:creator>
      <dc:date>2018-08-23T20:49:12Z</dc:date>
    </item>
    <item>
      <title>Re: IF statement inside EVAL</title>
      <link>https://community.splunk.com/t5/Splunk-Search/IF-statement-inside-EVAL/m-p/416511#M119873</link>
      <description>&lt;P&gt;The individual search works: &lt;/P&gt;

&lt;P&gt;| timechart count(Number) as CountEvents by sourcetype&lt;/P&gt;

&lt;P&gt;sourcetype="totaltru"&lt;BR /&gt;
| timechart sum(Total) as Total by Site &lt;/P&gt;

&lt;P&gt;but trying to include them in the same search in order to create a ratio is the problem. &lt;/P&gt;</description>
      <pubDate>Thu, 23 Aug 2018 20:54:43 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/IF-statement-inside-EVAL/m-p/416511#M119873</guid>
      <dc:creator>tonahoyos</dc:creator>
      <dc:date>2018-08-23T20:54:43Z</dc:date>
    </item>
  </channel>
</rss>

