# Details of Top N records

**URL:** <https://discuss.elastic.co/t/details-of-top-n-records/280224>\
**Category:** Elasticsearch\
**Created:** [August 2, 2021, 1:08pm UTC](https://discuss.elastic.co/t/details-of-top-n-records/280224 "2021-08-02T13:08:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ddramireddy](https://avatars.discourse-cdn.com/v4/letter/d/bc79bd/32.png) [@ddramireddy](https://discuss.elastic.co/u/ddramireddy)\
**Post date:** [August 2, 2021, 1:08pm UTC](https://discuss.elastic.co/t/details-of-top-n-records/280224/1 "2021-08-02T13:08:18Z")

</div>

Hi,

I have an index with following schema:

GroupName,Time,Value

Example values:

G1 2020-01-01 1:00:00. 10  
G2 2020-01-01 1:00:00. 15  
G3 2020-01-01 1:00:00. 20  
G1 2020-01-01 2:00:00. 10  
G2 2020-01-01 2:00:00. 20  
**G3 2020-01-01 2:00:00. 30**  
**G1 2020-01-01 3:00:00. 25**  
G2 2020-01-01 3:00:00. 10  
G3 2020-01-01 3:00:00. 10

Out of above records G3 and G1 has top 2 values 30 and 25 respectively. So, need the all records of G1 and G2.

G1 2020-01-01 1:00:00. 10  
G3 2020-01-01 1:00:00. 20  
G1 2020-01-01 2:00:00. 10  
G3 2020-01-01 2:00:00. 30  
G1 2020-01-01 3:00:00. 25  
G3 2020-01-01 3:00:00. 10

Is it possible to get this in single elastic query by using aggregations?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [August 3, 2021, 7:09am UTC](https://discuss.elastic.co/t/details-of-top-n-records/280224/2 "2021-08-03T07:09:21Z")

</div>

not on top of my head. You could go with a terms agg on the groupname field, that contains a `max` aggregation for the maximum value for each groupname and sort the terms agg based on that.

Then with a second query you could filter by groupname G1 or G3 if those are the top two max values from the first request.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 31, 2021, 7:10am UTC](https://discuss.elastic.co/t/details-of-top-n-records/280224/3 "2021-08-31T07:10:12Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
