<?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 to fetch only the top x rows for each category in the same search query? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-fetch-only-the-top-x-rows-for-each-category-in-the-same/m-p/386070#M168832</link>
    <description>&lt;P&gt;I am trying to fetch top 10 max Requests count of events with their corresponding response time. So using the below query:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=x host=prod* sourcetype=y 
|timechart span=1m count(Req) as Requests, sum(Resp_Time_MS) as "Total Response Time", max(Resp_Time_MS) as "Maximum Response Time", p95(Resp_Time_MS) as "95%ile of Response Time" 
|sort -Requests 
| head 10
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;So the result is:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;'_time' 'Requests' 'Total Response Time' 'Maximum Response Time' '95%ile of Response Time'
'2018-06-10 07:10:00'   '71653' '19141836'  '786602'      '560'
..
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The above query is giving me the top 10 highest Requests in common among all hosts. But I want top 10 highest values of Requests for each host (such as ProdA, ProdB, ProdC and ProdD). I have tried to group the results with the help of 'by' clause as "by host" but it is not giving the correct results.&lt;/P&gt;

&lt;P&gt;So expected result is:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;'_time' 'host' 'Requests' 'Total Response Time' 'Maximum Response Time' '95%ile of Response Time'
'2018-06-10 07:10:00' 'ProdA' '71653'   '19141836'  '786602'      '560'
'2018-06-10 07:05:00'   'ProdA' '58407' '21815806'  '1105451'   '626'
(Should display all 10 values)
'2018-06-18 06:59:00'   'ProdB'  '42013'    '18745773'  '894743'     '622'
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;So on.&lt;/P&gt;</description>
    <pubDate>Mon, 18 Jun 2018 07:40:59 GMT</pubDate>
    <dc:creator>akarivaratharaj</dc:creator>
    <dc:date>2018-06-18T07:40:59Z</dc:date>
    <item>
      <title>How to fetch only the top x rows for each category in the same search query?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-fetch-only-the-top-x-rows-for-each-category-in-the-same/m-p/386070#M168832</link>
      <description>&lt;P&gt;I am trying to fetch top 10 max Requests count of events with their corresponding response time. So using the below query:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=x host=prod* sourcetype=y 
|timechart span=1m count(Req) as Requests, sum(Resp_Time_MS) as "Total Response Time", max(Resp_Time_MS) as "Maximum Response Time", p95(Resp_Time_MS) as "95%ile of Response Time" 
|sort -Requests 
| head 10
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;So the result is:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;'_time' 'Requests' 'Total Response Time' 'Maximum Response Time' '95%ile of Response Time'
'2018-06-10 07:10:00'   '71653' '19141836'  '786602'      '560'
..
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The above query is giving me the top 10 highest Requests in common among all hosts. But I want top 10 highest values of Requests for each host (such as ProdA, ProdB, ProdC and ProdD). I have tried to group the results with the help of 'by' clause as "by host" but it is not giving the correct results.&lt;/P&gt;

&lt;P&gt;So expected result is:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;'_time' 'host' 'Requests' 'Total Response Time' 'Maximum Response Time' '95%ile of Response Time'
'2018-06-10 07:10:00' 'ProdA' '71653'   '19141836'  '786602'      '560'
'2018-06-10 07:05:00'   'ProdA' '58407' '21815806'  '1105451'   '626'
(Should display all 10 values)
'2018-06-18 06:59:00'   'ProdB'  '42013'    '18745773'  '894743'     '622'
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;So on.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Jun 2018 07:40:59 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-fetch-only-the-top-x-rows-for-each-category-in-the-same/m-p/386070#M168832</guid>
      <dc:creator>akarivaratharaj</dc:creator>
      <dc:date>2018-06-18T07:40:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to fetch only the top x rows for each category in the same search query?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-fetch-only-the-top-x-rows-for-each-category-in-the-same/m-p/386071#M168833</link>
      <description>&lt;P&gt;That will be a bit tricky, since &lt;CODE&gt;head&lt;/CODE&gt; doesn't support a by clause.&lt;/P&gt;

&lt;P&gt;To start, you would need to add &lt;CODE&gt;by host&lt;/CODE&gt; to your timechart command, to even make the host information available. Next step is to sort by host and within that sort  by Requests. Then you need to number the lines for each host and filter out the first 10.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; index=x host=prod* sourcetype=y
| timechart span=1m count(Req) as Requests, sum(Resp_Time_MS) as "Total Response Time", max(Resp_Time_MS) as "Maximum Response Time", p95(Resp_Time_MS) as "95%ile of Response Time" by host
| sort host,-Requests
| streamstats reset_on_change=true count by host
| where count&amp;lt;=10
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 18 Jun 2018 08:03:51 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-fetch-only-the-top-x-rows-for-each-category-in-the-same/m-p/386071#M168833</guid>
      <dc:creator>FrankVl</dc:creator>
      <dc:date>2018-06-18T08:03:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to fetch only the top x rows for each category in the same search query?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-fetch-only-the-top-x-rows-for-each-category-in-the-same/m-p/386072#M168834</link>
      <description>&lt;P&gt;Try like this&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=x host=prod* sourcetype=y 
| bucket span=1m _time
| stats count(Req) as Requests, sum(Resp_Time_MS) as "Total Response Time", max(Resp_Time_MS) as "Maximum Response Time", p95(Resp_Time_MS) as "95%ile of Response Time" by host _time
| sort 10 -Requests by host
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 18 Jun 2018 17:42:22 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-fetch-only-the-top-x-rows-for-each-category-in-the-same/m-p/386072#M168834</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2018-06-18T17:42:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to fetch only the top x rows for each category in the same search query?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-fetch-only-the-top-x-rows-for-each-category-in-the-same/m-p/386073#M168835</link>
      <description>&lt;P&gt;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/15147"&gt;@somesoni2&lt;/a&gt;, Awesome!! This is working fine. In addition to this I have added 'dedup', which is giving me top 10 Requests in each hosts &amp;amp; with their timestamp and response time.&lt;/P&gt;

&lt;P&gt;index=x host=prod* sourcetype=y &lt;BR /&gt;
 | bucket span=1m _time&lt;BR /&gt;
 | stats count(Req) as Requests, sum(Resp_Time_MS) as "Total Response Time", max(Resp_Time_MS) as "Maximum Response Time", p95(Resp_Time_MS) as "95%ile of Response Time" by host _time&lt;BR /&gt;
 | sort 10 -Requests by host&lt;BR /&gt;
 |dedup 10 host&lt;/P&gt;

&lt;P&gt;Thank you all for the help!!&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 20:01:55 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-fetch-only-the-top-x-rows-for-each-category-in-the-same/m-p/386073#M168835</guid>
      <dc:creator>akarivaratharaj</dc:creator>
      <dc:date>2020-09-29T20:01:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to fetch only the top x rows for each category in the same search query?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-fetch-only-the-top-x-rows-for-each-category-in-the-same/m-p/585542#M204009</link>
      <description>&lt;P&gt;Thank you for this one! Saved the day for me)&lt;/P&gt;</description>
      <pubDate>Thu, 17 Feb 2022 11:56:09 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-fetch-only-the-top-x-rows-for-each-category-in-the-same/m-p/585542#M204009</guid>
      <dc:creator>Shera</dc:creator>
      <dc:date>2022-02-17T11:56:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to fetch only the top x rows for each category in the same search query?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-fetch-only-the-top-x-rows-for-each-category-in-the-same/m-p/753514#M242736</link>
      <description>&lt;P&gt;I just realized that the key to the solution is &lt;FONT face="courier new,courier"&gt;sort&lt;/FONT&gt; + &lt;FONT face="courier new,courier"&gt;dedup&lt;/FONT&gt;. &amp;nbsp;sort alone would not fix the problem; in fact, sort 10 could misfire in some datasets, and by host is not a useful addition to sort. &amp;nbsp;So, the solution should be&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| sort -Requests
| dedup 10 host&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 24 Sep 2025 01:29:05 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-fetch-only-the-top-x-rows-for-each-category-in-the-same/m-p/753514#M242736</guid>
      <dc:creator>yuanliu</dc:creator>
      <dc:date>2025-09-24T01:29:05Z</dc:date>
    </item>
  </channel>
</rss>

