<?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: Can you help me get my subsearch to work? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Can-you-help-me-get-my-subsearch-to-work/m-p/382831#M111850</link>
    <description>&lt;P&gt;@keiran_harris, glad that it worked. If you do not have any further questions, you may accept it as answer to close the thread. I have updated the answer section&lt;/P&gt;</description>
    <pubDate>Wed, 02 Jan 2019 03:10:03 GMT</pubDate>
    <dc:creator>renjith_nair</dc:creator>
    <dc:date>2019-01-02T03:10:03Z</dc:date>
    <item>
      <title>Can you help me get my subsearch to work?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Can-you-help-me-get-my-subsearch-to-work/m-p/382826#M111845</link>
      <description>&lt;P&gt;Hi guys, &lt;/P&gt;

&lt;P&gt;i need help with a search. I believe it's a subsearch that i need (I need a variable output of one search to feed another search), but I cant make it work. &lt;/P&gt;

&lt;P&gt;Basically, i have written code that polls a weather forecast API and spits back JSON, which Splunk gobbles up. Trouble is, the API call is made several times a day, which means i get several, duplicate predictions in my data set. I only want to take the latest data, and ignore all previous. &lt;/P&gt;

&lt;P&gt;Here is my search which works well in giving me the table i need, when i have a clean index (i.e. only one API poll has been ingested thus far): &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;sourcetype=_jsonFUTURE BOMPREDdate | eval Day = strftime(_time,"%a") | eval Date = strftime(_time,"%F") | sort _time | table Day, Date, BOMPREDrainChance, BOMPREDrainMM, BOMPREDdescBrief, BOMPREDdescDetail | rename BOMPREDrainChance as "Rain%", BOMPREDrainMM as "RainMM", BOMPREDdescBrief as "ForecastBrief", BOMPREDdescDetail as "ForecastDetail"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;but when the API poll script has run twice, for instance, the table now has duplicates as shown below:&lt;BR /&gt;
&lt;span class="lia-inline-image-display-wrapper" image-alt="alt text"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/6318iAEFA8D121BDC44CF/image-size/large?v=v2&amp;amp;px=999" role="button" title="alt text" alt="alt text" /&gt;&lt;/span&gt;&lt;/P&gt;

&lt;P&gt;In my JSON data set, i have now included a field ive called 'BOMPREDapiPollTime' which is an epoch time that the script was executed...so the 7 JSON events that get ingested each time the script is run, all share the same value 'BOMPREDapiPollTime' as shown below. &lt;BR /&gt;
&lt;span class="lia-inline-image-display-wrapper" image-alt="alt text"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/6319iFAB9624804FBF7BD/image-size/large?v=v2&amp;amp;px=999" role="button" title="alt text" alt="alt text" /&gt;&lt;/span&gt;&lt;/P&gt;

&lt;P&gt;So, all i believe i need to do is: &lt;/P&gt;

&lt;P&gt;a) find that latest timestamp of 'BOMPREDapiPollTime'  - which i can do with the search 'sourcetype=_jsonFUTURE BOMPREDdate | stats latest(BOMPREDapiPollTime) as pollTime' &lt;/P&gt;

&lt;P&gt;b) feed that into my working search (pictured above) - i &lt;EM&gt;believe&lt;/EM&gt; with a subsearch... &lt;/P&gt;

&lt;P&gt;I have tried variants of the below without luck: &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;sourcetype=_jsonFUTURE BOMPREDdate [search sourcetype=_jsonFUTURE BOMPREDdate | stats latest(BOMPREDapiPollTime) as pollTime] | eval Day = strftime(_time,"%a") | eval Date = strftime(_time,"%F") | sort _time | table Day, Date, BOMPREDrainChance, BOMPREDrainMM, BOMPREDdescBrief, BOMPREDdescDetail | rename BOMPREDrainChance as "Rain%", BOMPREDrainMM as "RainMM", BOMPREDdescBrief as "ForecastBrief", BOMPREDdescDetail as "ForecastDetail"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;BUT I cant make it work!  (i always get zero results). &lt;/P&gt;

&lt;P&gt;Any help would be greatly appreciated. &lt;/P&gt;

&lt;P&gt;I'm sure it's something stupid I'm doing. &lt;/P&gt;

&lt;P&gt;thanks in advance guys! &lt;/P&gt;

&lt;P&gt;Keiran. &lt;/P&gt;</description>
      <pubDate>Sun, 30 Dec 2018 04:37:33 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Can-you-help-me-get-my-subsearch-to-work/m-p/382826#M111845</guid>
      <dc:creator>keiran_harris</dc:creator>
      <dc:date>2018-12-30T04:37:33Z</dc:date>
    </item>
    <item>
      <title>Re: Can you help me get my subsearch to work?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Can-you-help-me-get-my-subsearch-to-work/m-p/382827#M111846</link>
      <description>&lt;P&gt;@keiran_harris,&lt;BR /&gt;
&lt;STRONG&gt;Updated:&lt;/STRONG&gt;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; sourcetype=_jsonFUTURE BOMPREDdate | eval Day = strftime(_time,"%a") | eval Date = strftime(_time,"%F") 
 | table Day, Date, BOMPREDrainChance, BOMPREDrainMM, BOMPREDdescBrief, BOMPREDdescDetail,BOMPREDapiPollTime
 |eventstats latest(BOMPREDapiPollTime) as latestPollTime
 |where BOMPREDapiPollTime=latestPollTime
 | rename BOMPREDrainChance as "Rain%", BOMPREDrainMM as "RainMM", BOMPREDdescBrief as "ForecastBrief", BOMPREDdescDetail as "ForecastDetail"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;If you are looking for the latest records  (only one record ) for the day, try this&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;sourcetype=_jsonFUTURE BOMPREDdate |eval Date = strftime(_time,"%F")
|stats latest(BOMPREDrainChance) as "Rain%",latest(BOMPREDrainMM) as "RainMM",
       latest(BOMPREDdescBrief) as ForecastBrief,latest(BOMPREDdescDetail) as ForecastDetail by Date
|eval Day=strftime(strptime(Date,"%Y-%m-%d"),"%a")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Or&lt;/P&gt;

&lt;P&gt;If you are already sorting , sort in reverse order and get the &lt;CODE&gt;first&lt;/CODE&gt; value&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; sourcetype=_jsonFUTURE BOMPREDdate| eval Date = strftime(_time,"%F") | sort - _time 
 |stats first(BOMPREDrainChance) as "Rain%",first(BOMPREDrainMM) as "RainMM",
        first(BOMPREDdescBrief) as ForecastBrief,first(BOMPREDdescDetail) as ForecastDetail  by Date
|eval Day=strftime(strptime(Date,"%Y-%m-%d"),"%a")
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 30 Dec 2018 05:16:11 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Can-you-help-me-get-my-subsearch-to-work/m-p/382827#M111846</guid>
      <dc:creator>renjith_nair</dc:creator>
      <dc:date>2018-12-30T05:16:11Z</dc:date>
    </item>
    <item>
      <title>Re: Can you help me get my subsearch to work?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Can-you-help-me-get-my-subsearch-to-work/m-p/382828#M111847</link>
      <description>&lt;P&gt;Hi thanks for your help, but sorry if i didnt explian well.... i dont need just a single record.... each time the script runs, it generates 7 JSON events. And the table needs all 7, but only the latest 7. Each batch of 7 JSON events share the same API poll epoch time. Hopefully that clears up things?&lt;/P&gt;</description>
      <pubDate>Sun, 30 Dec 2018 05:54:49 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Can-you-help-me-get-my-subsearch-to-work/m-p/382828#M111847</guid>
      <dc:creator>keiran_harris</dc:creator>
      <dc:date>2018-12-30T05:54:49Z</dc:date>
    </item>
    <item>
      <title>Re: Can you help me get my subsearch to work?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Can-you-help-me-get-my-subsearch-to-work/m-p/382829#M111848</link>
      <description>&lt;P&gt;@keiran_harris , alright, so you just need the records which matches with the latest polltime and this polltime is same for all events (7).&lt;/P&gt;

&lt;P&gt;Try this,&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;sourcetype=_jsonFUTURE BOMPREDdate | eval Day = strftime(_time,"%a") | eval Date = strftime(_time,"%F") | table Day, Date, BOMPREDrainChance, BOMPREDrainMM, BOMPREDdescBrief, BOMPREDdescDetail,BOMPREDapiPollTime
|eventstats latest(BOMPREDapiPollTime) as latestPollTime
|where BOMPREDapiPollTime=latestPollTime
| rename BOMPREDrainChance as "Rain%", BOMPREDrainMM as "RainMM", BOMPREDdescBrief as "ForecastBrief", BOMPREDdescDetail as "ForecastDetail"
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 30 Dec 2018 06:57:14 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Can-you-help-me-get-my-subsearch-to-work/m-p/382829#M111848</guid>
      <dc:creator>renjith_nair</dc:creator>
      <dc:date>2018-12-30T06:57:14Z</dc:date>
    </item>
    <item>
      <title>Re: Can you help me get my subsearch to work?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Can-you-help-me-get-my-subsearch-to-work/m-p/382830#M111849</link>
      <description>&lt;P&gt;thanks!! that worked. Never used eventstats before - good to know! &lt;/P&gt;</description>
      <pubDate>Wed, 02 Jan 2019 01:50:40 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Can-you-help-me-get-my-subsearch-to-work/m-p/382830#M111849</guid>
      <dc:creator>keiran_harris</dc:creator>
      <dc:date>2019-01-02T01:50:40Z</dc:date>
    </item>
    <item>
      <title>Re: Can you help me get my subsearch to work?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Can-you-help-me-get-my-subsearch-to-work/m-p/382831#M111850</link>
      <description>&lt;P&gt;@keiran_harris, glad that it worked. If you do not have any further questions, you may accept it as answer to close the thread. I have updated the answer section&lt;/P&gt;</description>
      <pubDate>Wed, 02 Jan 2019 03:10:03 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Can-you-help-me-get-my-subsearch-to-work/m-p/382831#M111850</guid>
      <dc:creator>renjith_nair</dc:creator>
      <dc:date>2019-01-02T03:10:03Z</dc:date>
    </item>
  </channel>
</rss>

