# Using sub-aggregation result in parent

**URL:** <https://discuss.elastic.co/t/using-sub-aggregation-result-in-parent/63357>\
**Category:** Elasticsearch\
**Created:** [October 19, 2016, 8:43am UTC](https://discuss.elastic.co/t/using-sub-aggregation-result-in-parent/63357 "2016-10-19T08:43:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![frederes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/frederes/32/12476_2.png) [@frederes](https://discuss.elastic.co/u/frederes)\
**Post date:** [October 19, 2016, 8:43am UTC](https://discuss.elastic.co/t/using-sub-aggregation-result-in-parent/63357/1 "2016-10-19T08:43:07Z")

</div>

Hello,

Here is a sample request :

> ```
> {
> "query": {
> "term": {
> "campaign": "8"
> }
> },
> "size" : 0,
> "aggs": {
> "days" : {
> "date_histogram" : {
> "field" : "day",
> "interval" : "day",
> "format": "yyyy-MM-dd",
> "min_doc_count" : 1
> },
> "aggs": {
> "total_screens" : { "sum" : { "field" : "nbScreen" } },>         
> "pods": {
> "terms": {
> "field": "pod_name",
> "size": 0
> },
> "aggs": {
> "max_screens" : { "max" : { "field" : "nbScreen" } }
> }
> }
> }
> }
> }
> 
> ```
> 
> }

I would like in fact **the sum of max(nbScreen)** how can i do this ?  
Is it possible to use the result of a sub-aggregation as the input field of another aggregation ? (parent one)

---

<div class="post-metadata">

**Author:** ![frederes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/frederes/32/12476_2.png) [@frederes](https://discuss.elastic.co/u/frederes)\
**Post date:** [October 19, 2016, 9:31am UTC](https://discuss.elastic.co/t/using-sub-aggregation-result-in-parent/63357/2 "2016-10-19T09:31:50Z")

</div>

I found the "stats\_bucket" function that may help in my case, here is my query :

```
 {
    "query": {
        "term": {
            "campaign": "8"
        }
    },
    "size" : 0,
    "aggs": {
        "days" : {
            "date_histogram" : {
                "field" : "day",
                "interval" : "day",
                "format": "yyyy-MM-dd",
                "min_doc_count" : 1
            },
            "aggs": {
                "pods": {
                    "terms": {
                        "field": "pod_name",
                        "size": 0
                    },
                    "aggs": {
                        "max_screens" : { "max" : { "field" : "nbScreen" } }
                    }
                }
            }
        },
        "total_screens2": {
            "stats_bucket": {
                "buckets_path": "days>pods>max_screens"
            }
        }
    }
}

```

But i have an error on the "bukets\_path" :

```
{
   "error": {
      "root_cause": [],
      "type": "reduce_search_phase_exception",
      "reason": "[reduce] ",
      "phase": "fetch",
      "grouped": true,
      "failed_shards": [],
      "caused_by": {
         "type": "aggregation_execution_exception",
         "reason": "buckets_path must reference either a number value or a single value numeric metric aggregation, got: java.lang.Object[]"
      }
   },
   "status": 503
}

```

How can i specify this : days\>pods\>max\_screens ?

---

<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:11pm UTC](https://discuss.elastic.co/t/using-sub-aggregation-result-in-parent/63357/3 "2017-07-05T22:11:20Z")

</div>


