# Using top\_hits in sql

**URL:** <https://discuss.elastic.co/t/using-top-hits-in-sql/298517>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-sql\
**Created:** [March 1, 2022, 1:42pm UTC](https://discuss.elastic.co/t/using-top-hits-in-sql/298517 "2022-03-01T13:42:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nirf8](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nirf8/32/69962_2.png) [@Nirf8](https://discuss.elastic.co/u/Nirf8)\
**Post date:** [March 1, 2022, 1:42pm UTC](https://discuss.elastic.co/t/using-top-hits-in-sql/298517/1 "2022-03-01T13:42:54Z")

</div>

Hello,  
im using Elasticsearch sql to run aggregations on my data.  
What im trying to do is devide my data into buckets of time (using histogram) and then get the top docs of each bucket (using somthing similar to top\_hits).  
I have checked the sql documentation and especially the functions and operators page but didn't find a suitable solution.  
I did notice that functions like first\_value are implemented by top\_hits aggregation (can be seen when using the translate api).  
Is there a way, using sql syntax, to retrieve number of docs per bucket?

---

<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:** [March 29, 2022, 1:43pm UTC](https://discuss.elastic.co/t/using-top-hits-in-sql/298517/2 "2022-03-29T13:43:13Z")

</div>

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