<?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 use a different time range within a subsearch? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-use-a-different-time-range-within-a-subsearch/m-p/262082#M78683</link>
    <description>&lt;P&gt;Here is how I'll do&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=TEST CLASSNAME=JOB | stats count(eval(SITENAME == "PHX")) as Phoenix | eval Key="First Week"
| append [search index=TEST CLASSNAME=JOB [| gentimes start=-1 | addinfo | eval earliest=relative_time(info_min_time,"-7d") | eval latest=relative_time(info_max_time,"-7d") | table earliest latest ] |  stats count(eval(SITENAME == "PHX")) as Phoenix | eval Key="Second Week"] | table Key, Phoenix
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 08 Oct 2015 20:51:10 GMT</pubDate>
    <dc:creator>somesoni2</dc:creator>
    <dc:date>2015-10-08T20:51:10Z</dc:date>
    <item>
      <title>How to use a different time range within a subsearch?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-use-a-different-time-range-within-a-subsearch/m-p/262081#M78682</link>
      <description>&lt;P&gt;Splunk rookie here, so please be gentle.  I am hoping someone can help me with a date-time range issue within a subsearch.   My goal is to create a dashboard where you enter a date-time range (either from a time picker or something like the last 15 minutes), and then have it retrieve results for the current search as well as the same time range from a week ago.  The time range needs to be the difference between the "earliest" and "latest" values.   I'd like to expand this to show me the same time range from the current week, the previous week, then from 3 weeks back, and ending with 4 weeks back.  &lt;/P&gt;

&lt;P&gt;The problem I am having is passing the "earliest" and "latest" values to the subsearch.  For example, if earliest is &lt;CODE&gt;10/15/2015:10:00:00&lt;/CODE&gt; and latest is &lt;CODE&gt;10/15/2015:12:00:00&lt;/CODE&gt; (a 2 hour span), you would think these two values would be passed to the subsearch.  Then in the subsearch, you would think that &lt;CODE&gt;earliest=-7d&lt;/CODE&gt; would now be &lt;CODE&gt;10/8/2015:10:00:00&lt;/CODE&gt;, and &lt;CODE&gt;latest=-7d&lt;/CODE&gt; would now be  &lt;CODE&gt;10/8/2015:12:00:00&lt;/CODE&gt;, which is still a two hour time span, but for the previous week.  However, it looks like the earliest and latest values are being set to the same date-time value, thus returning zero results.  This seems to happen whether I specify weeks, days, minutes, seconds.  I also don't want to explicitly specify the values for earliest and latest in the subsearch unless it can be an assignment from a variable or just subtracting weeks,  days, hours, or minutes.  I have also tried  assigning the value of a variable to earliest and latest within the subsearch, but this does not seem possible.  &lt;/P&gt;

&lt;P&gt;Would someone have an idea of how to make what I am trying to do work?  Below is a code example (does not work) that shows what I was trying to do with variable assignment in the subsearch.  Thanks in advance for any assistance you can offer.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=TEST CLASSNAME=JOB | eval DiffTime=latest-earliest | eval BeginTime=earliest-DiffTime-604800 | 
eval CompleteTime=latest-604800 | stats count(eval(SITENAME == "PHX")) as Phoenix | eval Key="First Week"
append [ search index=TEST CLASSNAME=JOB earliest=-$BeginTime$ latest=-$CompleteTime$ | 
stats count(eval(SITENAME == "PHX")) as Phoenix | eval Key="Second Week" ] | table Key, Phoenix
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 08 Oct 2015 19:46:04 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-use-a-different-time-range-within-a-subsearch/m-p/262081#M78682</guid>
      <dc:creator>lblackey</dc:creator>
      <dc:date>2015-10-08T19:46:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to use a different time range within a subsearch?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-use-a-different-time-range-within-a-subsearch/m-p/262082#M78683</link>
      <description>&lt;P&gt;Here is how I'll do&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=TEST CLASSNAME=JOB | stats count(eval(SITENAME == "PHX")) as Phoenix | eval Key="First Week"
| append [search index=TEST CLASSNAME=JOB [| gentimes start=-1 | addinfo | eval earliest=relative_time(info_min_time,"-7d") | eval latest=relative_time(info_max_time,"-7d") | table earliest latest ] |  stats count(eval(SITENAME == "PHX")) as Phoenix | eval Key="Second Week"] | table Key, Phoenix
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 08 Oct 2015 20:51:10 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-use-a-different-time-range-within-a-subsearch/m-p/262082#M78683</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2015-10-08T20:51:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to use a different time range within a subsearch?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-use-a-different-time-range-within-a-subsearch/m-p/262083#M78684</link>
      <description>&lt;P&gt;Where,&lt;BR /&gt;
| gentimes - to get a single dummy rows for processing&lt;BR /&gt;
| addinfo - adds common search information, including search timerange to current search result (dummy row)&lt;BR /&gt;
info_min_time and info_max_time - are the fields added by addinfo command and denotes current search's earlieset and latest&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 07:32:18 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-use-a-different-time-range-within-a-subsearch/m-p/262083#M78684</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2020-09-29T07:32:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to use a different time range within a subsearch?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-use-a-different-time-range-within-a-subsearch/m-p/262084#M78685</link>
      <description>&lt;P&gt;Do it like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;|noop|stats count AS pos | eval pos="1,2" | makemv delim="," pos | mvexpand pos | addinfo | eval info_min_time=info_min_time - if((pos=1), 0, 7*24*60*60) | eval info_max_time=info_max_time - if((pos=1), 0, 7*24*60*60) | map search="search earliest=$info_min_time$ latest=$info_max_time$ | PUT YOUR SEARCH HERE"
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 08 Oct 2015 20:55:31 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-use-a-different-time-range-within-a-subsearch/m-p/262084#M78685</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2015-10-08T20:55:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to use a different time range within a subsearch?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-use-a-different-time-range-within-a-subsearch/m-p/262085#M78686</link>
      <description>&lt;P&gt;I was able to take the suggestion from woodcock, modify it from an "if" statement to a "case" statement, and got it to run showing a four week comparison.  Here is the search string:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| noop | stats count AS pos | eval pos="1,2,3,4" | makemv delim="," pos | mvexpand pos | addinfo | 
eval info_min_time=info_min_time - case(pos == 1, 0, pos == 2, 7*86400, pos == 3, 14*86400, pos == 4, 21*86400) | 
eval info_max_time=info_max_time - case(pos == 1, 0, pos == 2, 7*86400, pos == 3, 14*86400, pos == 4, 21*86400) | 
map search="search index=TEST CLASSNAME=JOB earliest=$info_min_time$ latest=$info_max_time$ | stats count by SITENAME" | 
sort str(SITENAME) | table SITENAME, count
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I had trouble figuring out the response from somesoni2.  If I paste the search string and run it in Splunk, it gives me an error of:&lt;BR /&gt;
"Error in 'search' command: Unable to parse the search: 'AND' operator is missing a clause on the left hand side".&lt;/P&gt;

&lt;P&gt;I found the results from woodcock's suggestion hard to work with.  As an example,  if "stats count by SITENAME" was moved outside of the quoted map search string, the subsearch results were truncated at 10000.  Moving it inside the quotes resolved this.  Here is the search string where the subsearch results were truncated:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| noop | stats count AS pos | eval pos="1,2,3,4" | makemv delim="," pos | mvexpand pos | addinfo | 
eval info_min_time=info_min_time - case(pos == 1, 0, pos == 2, 7*86400, pos == 3, 14*86400, pos == 4, 21*86400) | 
eval info_max_time=info_max_time - case(pos == 1, 0, pos == 2, 7*86400, pos == 3, 14*86400, pos == 4, 21*86400) | 
map search="search index=TEST  CLASSNAME=JOB earliest=$info_min_time$ latest=$info_max_time$" | 
convert timeformat="%Y-%m-%d" ctime(_time) AS date | stats count by SITENAME date
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Unfortunately, moving "stats count by SITENAME" within the map search string kept me from doing the time conversion  of _time to "date", and I could no longer get it to appear in my search results.  I even tried moving it within the map search string, but could not get it to work.&lt;/P&gt;

&lt;P&gt;Another interesting thing about woodcock's solution is that is jumps right into "Finalizing results", and zero events are found.  But I do see the correct count numbers in my search results.  I assume this is because the subsearch is doing all the work, and the parent search is what finds zero events?&lt;/P&gt;

&lt;P&gt;I would be interested in getting the response from somesoni2 to work using the append command with a subsearch.  Again, Splunk rookie here, so please hang in there with me.  I appreciate all the help you can offer.&lt;/P&gt;</description>
      <pubDate>Wed, 14 Oct 2015 21:13:43 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-use-a-different-time-range-within-a-subsearch/m-p/262085#M78686</guid>
      <dc:creator>lblackey</dc:creator>
      <dc:date>2015-10-14T21:13:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to use a different time range within a subsearch?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-use-a-different-time-range-within-a-subsearch/m-p/262086#M78687</link>
      <description>&lt;P&gt;Hi lblackey,&lt;/P&gt;

&lt;P&gt;if you just want to compare a count on days or weeks with previous days or weeks you could use the &lt;CODE&gt;timewrap&lt;/CODE&gt; app &lt;A href="https://splunkbase.splunk.com/app/1645/"&gt;https://splunkbase.splunk.com/app/1645/&lt;/A&gt; and run this search:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=TEST CLASSNAME=JOB SITENAME="PHX" earliest=-3w@w latest=-0w@w | timechart count | timewrap w
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This is just an example and I'm not sure if this is what you want....&lt;/P&gt;

&lt;P&gt;cheers, MuS&lt;/P&gt;</description>
      <pubDate>Wed, 14 Oct 2015 21:28:54 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-use-a-different-time-range-within-a-subsearch/m-p/262086#M78687</guid>
      <dc:creator>MuS</dc:creator>
      <dc:date>2015-10-14T21:28:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to use a different time range within a subsearch?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-use-a-different-time-range-within-a-subsearch/m-p/262087#M78688</link>
      <description>&lt;P&gt;I am not sure that I understand this comment of yours:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Unfortunately, moving "stats count by SITENAME" within the map search string kept me from doing the time conversion of _time to "date", and I could no longer get it to appear in my search results. I even tried moving it within the map search string, but could not get it to work.
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;But perhaps this twist on the solution works for you:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| noop | stats count AS pos | eval pos="1,2,3,4" | makemv delim="," pos | mvexpand pos | addinfo | eval info_min_time=info_min_time - case(pos == 1, 0, pos == 2, 7*86400, pos == 3, 14*86400, pos == 4, 21*86400) | eval info_max_time=info_max_time - case(pos == 1, 0, pos == 2, 7*86400, pos == 3, 14*86400, pos == 4, 21*86400) | map search="search index=TEST  CLASSNAME=JOB earliest=$info_min_time$ latest=$info_max_time$ | bucket _time span=1d | stats count by SITENAME _time" | stats count by SITENAME _time
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 16 Oct 2015 15:22:03 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-use-a-different-time-range-within-a-subsearch/m-p/262087#M78688</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2015-10-16T15:22:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to use a different time range within a subsearch?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-use-a-different-time-range-within-a-subsearch/m-p/262088#M78689</link>
      <description>&lt;P&gt;You should click "Accept" to close this Question and upvote the answers that helped you come up with your final solution&lt;/P&gt;</description>
      <pubDate>Sat, 08 Oct 2016 19:26:11 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-use-a-different-time-range-within-a-subsearch/m-p/262088#M78689</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2016-10-08T19:26:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to use a different time range within a subsearch?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-use-a-different-time-range-within-a-subsearch/m-p/262089#M78690</link>
      <description>&lt;P&gt;You should &lt;CODE&gt;UpVote&lt;/CODE&gt; any answers that were helpful and pick the best one and click &lt;CODE&gt;Accept&lt;/CODE&gt;.  The basic problem is that the values from &lt;CODE&gt;earliest&lt;/CODE&gt; and &lt;CODE&gt;latest&lt;/CODE&gt; coming from a subsearch &lt;EM&gt;must&lt;/EM&gt; be numbers (no relative time specifiers).&lt;/P&gt;</description>
      <pubDate>Mon, 10 Dec 2018 02:03:54 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-use-a-different-time-range-within-a-subsearch/m-p/262089#M78690</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2018-12-10T02:03:54Z</dc:date>
    </item>
  </channel>
</rss>

