<?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: Select the Top 1 from a set of TopN in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Select-the-Top-1-from-a-set-of-TopN/m-p/111199#M29134</link>
    <description>&lt;P&gt;Try this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Your base search | sort _time, -count | streamstats count as sno by _time | where sno &amp;lt;2
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;this gives top 1 from each hour. &lt;/P&gt;</description>
    <pubDate>Fri, 28 Mar 2014 12:56:28 GMT</pubDate>
    <dc:creator>somesoni2</dc:creator>
    <dc:date>2014-03-28T12:56:28Z</dc:date>
    <item>
      <title>Select the Top 1 from a set of TopN</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Select-the-Top-1-from-a-set-of-TopN/m-p/111195#M29130</link>
      <description>&lt;P&gt;Using the Splunk query language how would be a splunk query that returns the Top 1 from a set of Top N?&lt;/P&gt;

&lt;P&gt;Data set sample:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;time               Term         count
2014-03-28 10:00   hello        10
2014-03-28 10:00   ciao          9 
2014-03-28 10:00   nice          7
2014-03-28 11:00   nice         11
2014-03-28 11:00   great         8 
2014-03-28 11:00   precise       6
2014-03-28 12:00   yougotit      6
2014-03-28 12:00   ok            4 
2014-03-28 12:00   thanks        3
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The splunk query should return the top 1 of each Top N set. Example:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;time               Term         count
2014-03-28 10:00   hello        10
2014-03-28 11:00   nice         11
2014-03-28 12:00   yougotit      6
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Thanks,&lt;BR /&gt;
Lp&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;My solution:&lt;/STRONG&gt;&lt;/P&gt;

&lt;P&gt;After reading the suggestions provided in below answers,I took the following approach:&lt;/P&gt;

&lt;P&gt;1) Create a summary index.&lt;/P&gt;

&lt;P&gt;2) Create an hourly schedule search to get the Top N and store the results in the summary index. Splunk query:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index="my_raw_index" |eval time=strftime(_time, "%m/%d/%Y:%H:%M") |
top limit=0 term by time|streamstats count as rank|table time term count
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Result set:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;time              rank  Term         count
2014-03-28 10:00   1    hello        10
2014-03-28 10:00   2    nice         11
2014-03-28 10:00   3    yougotit      6
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;3) Then, by using the rank field, it is quite simple to get the Top 1 from the set of Top N result set from the summary index. Query example:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=my_summary_index rank=1|table time Term count.
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I think this approach would scale quite well.&lt;/P&gt;

&lt;P&gt;Thanks,&lt;BR /&gt;
Lp&lt;/P&gt;</description>
      <pubDate>Fri, 28 Mar 2014 11:26:47 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Select-the-Top-1-from-a-set-of-TopN/m-p/111195#M29130</guid>
      <dc:creator>lpolo</dc:creator>
      <dc:date>2014-03-28T11:26:47Z</dc:date>
    </item>
    <item>
      <title>Re: Select the Top 1 from a set of TopN</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Select-the-Top-1-from-a-set-of-TopN/m-p/111196#M29131</link>
      <description>&lt;P&gt;&lt;CODE&gt;your search|sort - time,count|dedup time,count&lt;/CODE&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 28 Mar 2014 11:53:26 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Select-the-Top-1-from-a-set-of-TopN/m-p/111196#M29131</guid>
      <dc:creator>linu1988</dc:creator>
      <dc:date>2014-03-28T11:53:26Z</dc:date>
    </item>
    <item>
      <title>Re: Select the Top 1 from a set of TopN</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Select-the-Top-1-from-a-set-of-TopN/m-p/111197#M29132</link>
      <description>&lt;P&gt;maybe this?&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... your search ... | stats max(count) as count by Term
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 28 Mar 2014 12:02:17 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Select-the-Top-1-from-a-set-of-TopN/m-p/111197#M29132</guid>
      <dc:creator>wpreston</dc:creator>
      <dc:date>2014-03-28T12:02:17Z</dc:date>
    </item>
    <item>
      <title>Re: Select the Top 1 from a set of TopN</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Select-the-Top-1-from-a-set-of-TopN/m-p/111198#M29133</link>
      <description>&lt;P&gt;or you take both examples and combine them like this &lt;CODE&gt;run everywhere example&lt;/CODE&gt;:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=_internal | bucket _time span=1h | eval myTime=_time | stats max(kbps) as max by series, myTime | sort - myTime, max | dedup myTime, max | eval myTime=strftime(myTime, "%F %T")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;this will give you the highest thruput per hour per &lt;CODE&gt;series&lt;/CODE&gt;. You have to adapt it to match your needs.&lt;/P&gt;

&lt;P&gt;cheers, MuS&lt;/P&gt;</description>
      <pubDate>Fri, 28 Mar 2014 12:32:18 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Select-the-Top-1-from-a-set-of-TopN/m-p/111198#M29133</guid>
      <dc:creator>MuS</dc:creator>
      <dc:date>2014-03-28T12:32:18Z</dc:date>
    </item>
    <item>
      <title>Re: Select the Top 1 from a set of TopN</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Select-the-Top-1-from-a-set-of-TopN/m-p/111199#M29134</link>
      <description>&lt;P&gt;Try this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Your base search | sort _time, -count | streamstats count as sno by _time | where sno &amp;lt;2
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;this gives top 1 from each hour. &lt;/P&gt;</description>
      <pubDate>Fri, 28 Mar 2014 12:56:28 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Select-the-Top-1-from-a-set-of-TopN/m-p/111199#M29134</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2014-03-28T12:56:28Z</dc:date>
    </item>
    <item>
      <title>Re: Select the Top 1 from a set of TopN</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Select-the-Top-1-from-a-set-of-TopN/m-p/111200#M29135</link>
      <description>&lt;P&gt;I use this one for hourly ranking, just to share:&lt;BR /&gt;
I am assuming you have multiple same terms in an hour so I have "stats max(count) by ..." , but in other case, please change it to fit your need...&lt;/P&gt;

&lt;P&gt;index="mine" filter_event&lt;BR /&gt;
| bucket _time span=1h&lt;BR /&gt;
| stats max(count) as count by term _time &lt;BR /&gt;
| sort - count&lt;BR /&gt;
| eval rank=1 &lt;BR /&gt;
| streamstats sum(rank) as rank by _time &lt;BR /&gt;
| where rank&amp;lt;4&lt;BR /&gt;
| xyseries _time rank term&lt;/P&gt;

&lt;P&gt;This gives you top 3 for each hour.&lt;BR /&gt;
Change where rank&amp;lt;4 to rank=1 or so to fit your need... and see how it goes...&lt;/P&gt;</description>
      <pubDate>Sat, 29 Mar 2014 04:37:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Select-the-Top-1-from-a-set-of-TopN/m-p/111200#M29135</guid>
      <dc:creator>melonman</dc:creator>
      <dc:date>2014-03-29T04:37:34Z</dc:date>
    </item>
    <item>
      <title>Re: Select the Top 1 from a set of TopN</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Select-the-Top-1-from-a-set-of-TopN/m-p/111201#M29136</link>
      <description>&lt;P&gt;What about &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | top 1 count by Term
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 29 Mar 2014 21:24:33 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Select-the-Top-1-from-a-set-of-TopN/m-p/111201#M29136</guid>
      <dc:creator>Runals</dc:creator>
      <dc:date>2014-03-29T21:24:33Z</dc:date>
    </item>
  </channel>
</rss>

