# How to get the data with concept in SQL "Group by"

**URL:** <https://discuss.elastic.co/t/how-to-get-the-data-with-concept-in-sql-group-by/270529>\
**Category:** Elasticsearch\
**Created:** [April 19, 2021, 4:33am UTC](https://discuss.elastic.co/t/how-to-get-the-data-with-concept-in-sql-group-by/270529 "2021-04-19T04:33:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phoebe\_Wong](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/phoebe_wong/32/79564_2.png) [@Phoebe\_Wong](https://discuss.elastic.co/u/Phoebe_Wong)\
**Post date:** [April 19, 2021, 4:33am UTC](https://discuss.elastic.co/t/how-to-get-the-data-with-concept-in-sql-group-by/270529/1 "2021-04-19T04:33:13Z")

</div>

Hi every one,

I would like to query the data by using the concept in SQL "Group by" like below

"SELECT user, DATE(timestamp), EXTRACT(HOUR FROM timestamp), floor\_name FROM location WHERE DATE(timestamp) BETWEEN "(d1) " AND "(d2) " GROUP BY DATEtimestamp), EXTRACT(HOUR FROM timestamp), user, floor\_name"

However in Elasticsearch, no matter I use "terms aggregation" or "date\_histogram", I got the count number....while I want to query the data than the count like below

user | date| hour |floor\_name  
Jason| 2020-04-19 | 7 | Floor 1  
Jason| 2020-04-19 | 8| Floor 1  
Jason| 2020-04-19 | 9| Floor 2  
Louisa | 2020-04-19 | 8 | Floor 2  
Louisa | 2020-04-19 | 9| Floor 5

group by the date hour of a user.

May I know how to write a ES queries to give the above result ? many thanks.

---

<div class="post-metadata">

**Author:** ![matriv](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matriv/32/43656_2.png) [@matriv](https://discuss.elastic.co/u/matriv)\
**Post date:** [April 20, 2021, 7:48am UTC](https://discuss.elastic.co/t/how-to-get-the-data-with-concept-in-sql-group-by/270529/2 "2021-04-20T07:48:53Z")

</div>

> [@Phoebe\_Wong](#):
>
> May I know how to write a ES queries to give the above result ? many thanks.

The SQL Group By queries are translated to [composite aggregations](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-composite-aggregation.html). You could use the [translate SQL endpoint](https://www.elastic.co/guide/en/elasticsearch/reference/current/sql-translate.html) to see how your SQL query translates to ES query DSL.

---

<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:** [May 18, 2021, 7:49am UTC](https://discuss.elastic.co/t/how-to-get-the-data-with-concept-in-sql-group-by/270529/3 "2021-05-18T07:49:19Z")

</div>

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