# Aggregate the results of all sub aggregations

**URL:** <https://discuss.elastic.co/t/aggregate-the-results-of-all-sub-aggregations/51328>\
**Category:** Elasticsearch\
**Created:** [May 30, 2016, 1:39pm UTC](https://discuss.elastic.co/t/aggregate-the-results-of-all-sub-aggregations/51328 "2016-05-30T13:39:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sen](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@sen](https://discuss.elastic.co/u/sen)\
**Post date:** [May 30, 2016, 1:39pm UTC](https://discuss.elastic.co/t/aggregate-the-results-of-all-sub-aggregations/51328/1 "2016-05-30T13:39:31Z")

</div>

Hello everyone,

Here is what I would like to do. I have an index with several  
documents in Elasticsearch. In every document I have two field:  
deviceField (name of the device) and pressionField (the value of the  
pression periodically). I want to query in my index the average pression  
per device and to do some operations on it. Then, i want to sum the result for all device.  
I managed to do the operations on every device (code below) but not for the aggregation of the results of all sub-aggregation.

curl -XGET 'localhost:9200/mydata/\_search?pretty' -d '  
{  
"aggs" : {  
"devices" : {  
"terms" : {  
"field" : "deviceField"  
},  
"aggs" : {  
"average\_pression" : {  
"sum" : {  
"field" : "pressionField"  
}  
},  
"count\_frames" : {  
"value\_count" : {  
"field" : "deviceField"  
}  
},  
"per\_percentage": {  
"bucket\_script": {  
"buckets\_path": {  
"countFrames": "count\_frames",  
"average\_pression": "average\_pression"  
},  
"script": "average\_pression\*countFrames"  
}  
}  
}  
}  
}  
}'

Thank you very much for your attention and your help.

S

---

<div class="post-metadata">

**Author:** ![sen](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@sen](https://discuss.elastic.co/u/sen)\
**Post date:** [May 30, 2016, 2:45pm UTC](https://discuss.elastic.co/t/aggregate-the-results-of-all-sub-aggregations/51328/2 "2016-05-30T14:45:12Z")

</div>

I finally found the answer.

here is the code  
curl -XGET 'localhost:9200/mydata/\_search?pretty' -d '  
{  
"aggs" : {  
"devices" : {  
"terms" : {  
"field" : "deviceField"  
},  
"aggs" : {  
"average\_pression" : {  
"sum" : {  
"field" : "pressionField"  
}  
},  
"count\_frames" : {  
"value\_count" : {  
"field" : "deviceField"  
}  
},  
"per\_percentage": {  
"bucket\_script": {  
"buckets\_path": {  
"countFrames": "count\_frames",  
"average\_pression": "average\_pression"  
},  
"script": "average\_pression\*countFrames"  
},  
"avg\_per\_all\_devices":{  
"avg\_bucket":{  
"buckets\_path":"devices\>per\_percentage"  
}  
}  
}  
}  
}  
}  
}'

Good luck !  
S

---

<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:** [July 5, 2017, 10:47pm UTC](https://discuss.elastic.co/t/aggregate-the-results-of-all-sub-aggregations/51328/3 "2017-07-05T22:47:57Z")

</div>


