Sum across Average of buckets for Metric

I have data in elastic search that I want to split into buckets based on an ID, average values within each bucket and then sum the values between buckets. I have searched about nested aggregations from the elastic search website, but it seems like this sort of map reducing functionality may not be supported.

Can anyone confirm for me if this sort of script can be written in Elasticsearch?

You're probably after pipeline aggs, aggs on aggs, that is in 2.0.