<?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 How do I compare two time slices? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-do-I-compare-two-time-slices/m-p/389735#M113552</link>
    <description>&lt;P&gt;I have a search that I want to run twice, but for different time slices. The result of the two slices will then be compared to get a measure of the difference. My current code has the same search twice but with different &lt;EM&gt;earliest&lt;/EM&gt; and &lt;EM&gt;latest&lt;/EM&gt; values, associated using &lt;EM&gt;appendcols&lt;/EM&gt;. What I want to know is if there is a way to write the search once (instead of twice) and reuse that code; something like a common table expression in SQL?&lt;/P&gt;</description>
    <pubDate>Mon, 24 Sep 2018 19:52:26 GMT</pubDate>
    <dc:creator>asturt</dc:creator>
    <dc:date>2018-09-24T19:52:26Z</dc:date>
    <item>
      <title>How do I compare two time slices?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-do-I-compare-two-time-slices/m-p/389735#M113552</link>
      <description>&lt;P&gt;I have a search that I want to run twice, but for different time slices. The result of the two slices will then be compared to get a measure of the difference. My current code has the same search twice but with different &lt;EM&gt;earliest&lt;/EM&gt; and &lt;EM&gt;latest&lt;/EM&gt; values, associated using &lt;EM&gt;appendcols&lt;/EM&gt;. What I want to know is if there is a way to write the search once (instead of twice) and reuse that code; something like a common table expression in SQL?&lt;/P&gt;</description>
      <pubDate>Mon, 24 Sep 2018 19:52:26 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-do-I-compare-two-time-slices/m-p/389735#M113552</guid>
      <dc:creator>asturt</dc:creator>
      <dc:date>2018-09-24T19:52:26Z</dc:date>
    </item>
    <item>
      <title>Re: How do I compare two time slices?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-do-I-compare-two-time-slices/m-p/389736#M113553</link>
      <description>&lt;P&gt;Appendcols is almost never the right answer. NO, there is no CTE in Splunk...you can achieve some of that with macros, but you don't need to for this use case.  &lt;/P&gt;

&lt;P&gt;Here's the pseudocode you need - &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;your search that gets ALL the records for both (or all)  time periods 
| fields  ...list the fields you need...
| eval timeframe= case(this is the first time frame, "flag1", 
                       this is the second time frame, "flag2", 
                       ...for however many timeframes you want)
| where isnotnull(timeframe)

| now do all your processing and setup for the records

| now do summary processing and presentation
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;HR /&gt;

&lt;P&gt;1) Note that any records which don't match one of your desired timeframes get dropped. &lt;/P&gt;

&lt;P&gt;2) If you are going to do a timechart, then you have two options.  First, if appropriate, you can use the timewrap facility, or second, you can add time to all the earlier timeframes to make them align exactly with the current timeframe.  Either method works.&lt;/P&gt;</description>
      <pubDate>Tue, 25 Sep 2018 14:43:52 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-do-I-compare-two-time-slices/m-p/389736#M113553</guid>
      <dc:creator>DalJeanis</dc:creator>
      <dc:date>2018-09-25T14:43:52Z</dc:date>
    </item>
    <item>
      <title>Re: How do I compare two time slices?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-do-I-compare-two-time-slices/m-p/389737#M113554</link>
      <description>&lt;P&gt;You should use relative_time against each time slice. This will give you epoch time and you can compare against that &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval today=relative_time(now(), "0d@d")
| eval yesterday=relative_time(today, "-1d@d")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;A href="http://docs.splunk.com/Documentation/Splunk/7.1.3/SearchReference/DateandTimeFunctions"&gt;http://docs.splunk.com/Documentation/Splunk/7.1.3/SearchReference/DateandTimeFunctions&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Sep 2018 15:11:43 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-do-I-compare-two-time-slices/m-p/389737#M113554</guid>
      <dc:creator>skoelpin</dc:creator>
      <dc:date>2018-09-25T15:11:43Z</dc:date>
    </item>
    <item>
      <title>Re: How do I compare two time slices?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-do-I-compare-two-time-slices/m-p/389738#M113555</link>
      <description>&lt;P&gt;You can give the same base search 2 different &lt;CODE&gt;earliest/latest&lt;/CODE&gt; specifiers like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=YouShouldAlwaysSpecifyYourIndex AND sourcetype=AndSourcetypeToo
((earliest=@d latest=@d+1h) OR (earliest=-1d@d latest=-1d@d+1h))
| eval which=if(_time&amp;lt;relative_time(now(), "@d"), "yesterday", "today")
| stats count by which
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 25 Sep 2018 17:37:06 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-do-I-compare-two-time-slices/m-p/389738#M113555</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2018-09-25T17:37:06Z</dc:date>
    </item>
    <item>
      <title>Re: How do I compare two time slices?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-do-I-compare-two-time-slices/m-p/389739#M113556</link>
      <description>&lt;P&gt;Thank you. That's an elegant method for restricting the search to a single scan of the data.&lt;/P&gt;</description>
      <pubDate>Wed, 26 Sep 2018 20:09:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-do-I-compare-two-time-slices/m-p/389739#M113556</guid>
      <dc:creator>asturt</dc:creator>
      <dc:date>2018-09-26T20:09:34Z</dc:date>
    </item>
  </channel>
</rss>

