# Sub aggregations

**URL:** <https://discuss.elastic.co/t/sub-aggregations/83997>\
**Category:** Elasticsearch\
**Created:** [April 28, 2017, 10:27am UTC](https://discuss.elastic.co/t/sub-aggregations/83997 "2017-04-28T10:27:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jlarribe](https://avatars.discourse-cdn.com/v4/letter/j/a4c791/32.png) [@jlarribe](https://discuss.elastic.co/u/jlarribe)\
**Post date:** [April 28, 2017, 10:27am UTC](https://discuss.elastic.co/t/sub-aggregations/83997/1 "2017-04-28T10:27:54Z")

</div>

Hello, my question is about aggregation and sub aggregation.  
I would like to use ES as a replacement of my aggregation system (classical SGBRr).  
With a classic SQL language, it is possible to do a "group by" on more than one field.  
Example :  
SELECT Continent, Country, Sum(Visits) Visits  
FROM TABLE  
GROUP BY Continent, Country  
ORDER BY Visits DESC  
LIMIT 5

Here i want the top 5 couples of "Continent, Country" ordered by descending visits.

With ES, i do a first aggregation of the Continent dimension, and then a sub aggregation on the Country dimension. But i have no guarantee to get the same result than with a group by on two columns.  
Would it be possible with the future SQL language directly accessible on ES ?

Thanks,

Julien

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [May 3, 2017, 8:17pm UTC](https://discuss.elastic.co/t/sub-aggregations/83997/2 "2017-05-03T20:17:28Z")

</div>

Regarding SQL with Elasticsearch, this may interest you: [https://www.elastic.co/elasticon/conf/2017/sf/elasticsearch-sql](https://www.elastic.co/elasticon/conf/2017/sf/elasticsearch-sql)

> [@jlarribe](#):
>
> With ES, i do a first aggregation of the Continent dimension, and then a sub aggregation on the Country dimension. But i have no guarantee to get the same result than with a group by on two columns.

I'm not sure I understand this, however. Nesting aggregations in this manner will give you the same data as a SQL group on two fields, albeit in a slightly different format (nested trees rather than rows).

---

<div class="post-metadata">

**Author:** ![basharz](https://avatars.discourse-cdn.com/v4/letter/b/e99b99/32.png) [@basharz](https://discuss.elastic.co/u/basharz)\
**Post date:** [May 4, 2017, 6:36am UTC](https://discuss.elastic.co/t/sub-aggregations/83997/3 "2017-05-04T06:36:12Z")

</div>

try this hope it will help you  
{  
"size": 0,

"aggs": {  
"CityAgg": {  
"terms": {  
"field": "city.raw",  
"size": 5,  
"order": {  
"SumPriceCity": "desc"  
}  
},  
"aggs": {  
"SumPriceCity": {  
"sum": {  
"field": "price"  
}  
},  
"AreaAgg": {  
"terms": {  
"field": "area",  
"size": 5,  
"order": {  
"sumpricearea": "desc"  
}  
},  
"aggs": {  
"sumpricearea": {  
"sum": {  
"field": "price"  
}  
}  
}  
}  
}  
}  
}  
}

---

<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:** [June 1, 2017, 6:46am UTC](https://discuss.elastic.co/t/sub-aggregations/83997/4 "2017-06-01T06:46:23Z")

</div>

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