# Sum aggregation with results from other aggregations?

**URL:** <https://discuss.elastic.co/t/sum-aggregation-with-results-from-other-aggregations/23740>\
**Category:** Elasticsearch\
**Created:** [June 1, 2015, 7:16pm UTC](https://discuss.elastic.co/t/sum-aggregation-with-results-from-other-aggregations/23740 "2015-06-01T19:16:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josh\_Harrison](https://avatars.discourse-cdn.com/v4/letter/j/0ea827/32.png) [@Josh\_Harrison](https://discuss.elastic.co/u/Josh_Harrison)\
**Post date:** [June 1, 2015, 7:16pm UTC](https://discuss.elastic.co/t/sum-aggregation-with-results-from-other-aggregations/23740/1 "2015-06-01T19:16:44Z")

</div>

Is it possible to create an aggregation where I can do a sum on the results  
of a sub bucket?

I'm working on twitter data. In this data I have a bunch of retweets of  
different users.  
Say that user A has 10 tweets that are retweeted a hundred times in my  
dataset. I want to find the maximum retweet\_count for each individual  
tweet, and then I want to find the sum of all of those maximums from an  
individual user.  
This is the base query structure I'm working with:

{  
"aggs": {  
"user\_id": {  
"terms": {  
"field": "retweet\_user\_id"  
},  
"aggs": {  
"tweet\_ids": {  
"terms": {  
"field": "retweet\_id",  
"order": "max\_tweet.value"  
},  
"aggs": {  
"max\_tweet": {  
"max": {  
"field": "retweet\_count"  
}  
}  
}  
}  
}  
}  
}  
}

Importantly here, I don't want to just take a sum of "retweet\_count" for a given retweet\_user\_id - this doesn't give the max value per tweet.

Essentially, is it possible for me to take a sum of the agg results at user\_id.tweet\_ids.max\_tweet.value, and use that as an "order" term in the user\_id terms agg?

## -- Please update your bookmarks! We have moved to [https://discuss.elastic.co/](https://discuss.elastic.co/)

You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/100b463d-0f95-4801-aec7-e32544624518%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/100b463d-0f95-4801-aec7-e32544624518%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 6, 2017, 12:10am UTC](https://discuss.elastic.co/t/sum-aggregation-with-results-from-other-aggregations/23740/2 "2017-07-06T00:10:21Z")

</div>


