<?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 Subsearch with stats first not working as expected in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Subsearch-with-stats-first-not-working-as-expected/m-p/130174#M35439</link>
    <description>&lt;P&gt;My dataset has multiple events for a single _time. Batches get loaded whenever it's sent by a 3rd party. I have 25 unique sites that gets data sent. I have a query which finds the most recent _time for the data by site_code:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=tgem sourcetype=tgem | stats first(_time) as _time by site_code
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;It works fine, and returns the expected value, which is 2014-01-12 for the site_code=BR. So far so good, so now I want all matching records for that _time, so I glue it into a subquery like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=tgem sourcetype=tgem [search index=tgem sourcetype=tgem | stats first(_time) as _time by site_code | return 25 _time] | search site_code=BR
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;It returns all records for 2014-01-04, which is the earliest _time in my period (month to date). Any ideas? I've been pulling my hair out all day.&lt;/P&gt;

&lt;P&gt;My requirement is to do some stats (min, max, avg) of a field in the dataset, by site_code, for wahtever the most recent set of data is for that site_code&lt;/P&gt;</description>
    <pubDate>Mon, 28 Sep 2020 15:45:21 GMT</pubDate>
    <dc:creator>robertlabrie</dc:creator>
    <dc:date>2020-09-28T15:45:21Z</dc:date>
    <item>
      <title>Subsearch with stats first not working as expected</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Subsearch-with-stats-first-not-working-as-expected/m-p/130174#M35439</link>
      <description>&lt;P&gt;My dataset has multiple events for a single _time. Batches get loaded whenever it's sent by a 3rd party. I have 25 unique sites that gets data sent. I have a query which finds the most recent _time for the data by site_code:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=tgem sourcetype=tgem | stats first(_time) as _time by site_code
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;It works fine, and returns the expected value, which is 2014-01-12 for the site_code=BR. So far so good, so now I want all matching records for that _time, so I glue it into a subquery like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=tgem sourcetype=tgem [search index=tgem sourcetype=tgem | stats first(_time) as _time by site_code | return 25 _time] | search site_code=BR
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;It returns all records for 2014-01-04, which is the earliest _time in my period (month to date). Any ideas? I've been pulling my hair out all day.&lt;/P&gt;

&lt;P&gt;My requirement is to do some stats (min, max, avg) of a field in the dataset, by site_code, for wahtever the most recent set of data is for that site_code&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 15:45:21 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Subsearch-with-stats-first-not-working-as-expected/m-p/130174#M35439</guid>
      <dc:creator>robertlabrie</dc:creator>
      <dc:date>2020-09-28T15:45:21Z</dc:date>
    </item>
    <item>
      <title>Re: Subsearch with stats first not working as expected</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Subsearch-with-stats-first-not-working-as-expected/m-p/130175#M35440</link>
      <description>&lt;P&gt;Use max(_time) instead of first(_time)&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 15:45:38 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Subsearch-with-stats-first-not-working-as-expected/m-p/130175#M35440</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2020-09-28T15:45:38Z</dc:date>
    </item>
    <item>
      <title>Re: Subsearch with stats first not working as expected</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Subsearch-with-stats-first-not-working-as-expected/m-p/130176#M35441</link>
      <description>&lt;P&gt;For debugging subsearches you can take a look at the job inspector. That will show you what the subsearch evaluated to, giving you a chance to narrow the problem to either the subsearch or the outer search.&lt;/P&gt;

&lt;P&gt;A different thought, merge the site_code filter into the first search command for potentially greater performance - unrelated to the results though.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jan 2014 23:30:59 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Subsearch-with-stats-first-not-working-as-expected/m-p/130176#M35441</guid>
      <dc:creator>martin_mueller</dc:creator>
      <dc:date>2014-01-29T23:30:59Z</dc:date>
    </item>
    <item>
      <title>Re: Subsearch with stats first not working as expected</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Subsearch-with-stats-first-not-working-as-expected/m-p/130177#M35442</link>
      <description>&lt;P&gt;The &lt;CODE&gt;first&lt;/CODE&gt; and &lt;CODE&gt;last&lt;/CODE&gt; functions are relative to the order of events - when you are dealing with time, &lt;CODE&gt;earliest&lt;/CODE&gt; and &lt;CODE&gt;latest&lt;/CODE&gt; are usually better. Also, you need to return the site code as well as the time; you don't need the &lt;CODE&gt;return&lt;/CODE&gt; comand.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=tgem sourcetype=tgem [search  index=tgem sourcetype=tgem | stats latest(_time) as _time by site_code ]
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Use the search job inspector to see how the subsearch values are returned.&lt;/P&gt;</description>
      <pubDate>Thu, 30 Jan 2014 06:23:38 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Subsearch-with-stats-first-not-working-as-expected/m-p/130177#M35442</guid>
      <dc:creator>lguinn2</dc:creator>
      <dc:date>2014-01-30T06:23:38Z</dc:date>
    </item>
    <item>
      <title>Re: Subsearch with stats first not working as expected</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Subsearch-with-stats-first-not-working-as-expected/m-p/130178#M35443</link>
      <description>&lt;P&gt;So in the end two things happened.&lt;/P&gt;

&lt;OL&gt;
&lt;LI&gt;_time never worked for me as expected. I used another field in the data which uniquely identified the batch.&lt;/LI&gt;
&lt;LI&gt;I used format to customize the subsearch expansion. The job inspector was king to sorting that out. Seeing how the subsearch expanded.&lt;/LI&gt;
&lt;/OL&gt;

&lt;P&gt;The format sting I used was&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;format "(" "(" "AND" ")" "OR" ")"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Thanks to everyone for your replies.&lt;/P&gt;</description>
      <pubDate>Tue, 04 Feb 2014 21:03:26 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Subsearch-with-stats-first-not-working-as-expected/m-p/130178#M35443</guid>
      <dc:creator>robertlabrie</dc:creator>
      <dc:date>2014-02-04T21:03:26Z</dc:date>
    </item>
  </channel>
</rss>

