# How to write a query with groupBy, having and orderBy with elastic?

**URL:** <https://discuss.elastic.co/t/how-to-write-a-query-with-groupby-having-and-orderby-with-elastic/151313>\
**Category:** Elasticsearch\
**Created:** [October 6, 2018, 11:02am UTC](https://discuss.elastic.co/t/how-to-write-a-query-with-groupby-having-and-orderby-with-elastic/151313 "2018-10-06T11:02:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ali\_houshyaripour](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ali_houshyaripour/32/35931_2.png) [@ali\_houshyaripour](https://discuss.elastic.co/u/ali_houshyaripour)\
**Post date:** [October 6, 2018, 11:02am UTC](https://discuss.elastic.co/t/how-to-write-a-query-with-groupby-having-and-orderby-with-elastic/151313/1 "2018-10-06T11:02:35Z")

</div>

Hi,

I want to write a query in elastic that contain these 3 parts like sql:

- groupBy
- having
- orderBy

How can I write it?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [October 6, 2018, 1:18pm UTC](https://discuss.elastic.co/t/how-to-write-a-query-with-groupby-having-and-orderby-with-elastic/151313/2 "2018-10-06T13:18:07Z")

</div>

In elasticsearch you write a search request. It can have:

Group by = the aggregation part  
Having = the query part  
OrderBy = the sort part

---

<div class="post-metadata">

**Author:** ![Andrei\_Stefan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrei_stefan/32/47533_2.png) [@Andrei\_Stefan](https://discuss.elastic.co/u/Andrei_Stefan)\
**Post date:** [October 8, 2018, 1:01pm UTC](https://discuss.elastic.co/t/how-to-write-a-query-with-groupby-having-and-orderby-with-elastic/151313/3 "2018-10-08T13:01:52Z")

</div>

Hi @ali_houshyaripour,  
If you are just starting your journey with Elasticsearch, it might help you the ES-SQL [`translate` API](https://www.elastic.co/guide/en/elasticsearch/reference/6.x/sql-translate.html). You can pass an SQL query to it (with some limitations) and it can suggest an ES query. This might help you get started with Elasticsearch.

---

<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:** [November 5, 2018, 1:03pm UTC](https://discuss.elastic.co/t/how-to-write-a-query-with-groupby-having-and-orderby-with-elastic/151313/4 "2018-11-05T13:03:54Z")

</div>

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