# Reference Result of Aggregation in Sub-Aggregation

**URL:** <https://discuss.elastic.co/t/reference-result-of-aggregation-in-sub-aggregation/205425>\
**Category:** Elasticsearch\
**Created:** [October 28, 2019, 8:53am UTC](https://discuss.elastic.co/t/reference-result-of-aggregation-in-sub-aggregation/205425 "2019-10-28T08:53:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gose](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gose/32/37875_2.png) [@gose](https://discuss.elastic.co/u/gose)\
**Post date:** [October 28, 2019, 8:53am UTC](https://discuss.elastic.co/t/reference-result-of-aggregation-in-sub-aggregation/205425/1 "2019-10-28T08:53:31Z")

</div>

How do I refer to "avg\_flight\_min" in the Range Aggregation, replacing the hard-coded value of 100:

```
GET kibana_sample_data_flights/_search
{
  "size": 0,
  "aggs": {
    "avg_flight_min": {
      "avg": {
        "field": "FlightTimeMin"
      }
    },
    "price_ranges": {
      "range": {
        "field": "FlightTimeMin",
        "keyed": true, 
        "ranges": [
          {
            "key": "below_avg", 
            "to": 100
          },
          {
            "key": "above_avg", 
            "from": 100
          }
        ]
      }
    }
  }
}

```

I am using the Sample Flight Data in this example.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [October 31, 2019, 1:15pm UTC](https://discuss.elastic.co/t/reference-result-of-aggregation-in-sub-aggregation/205425/2 "2019-10-31T13:15:10Z")

</div>

You would need to run two queries for this. First a search for the avg aggregation, than a search for the for the range.

For some use-cases there are [pipelines aggregations](https://www.elastic.co/guide/en/elasticsearch/reference/7.4/search-aggregations-pipeline.html), that use the output of aggregations as their input.

---

<div class="post-metadata">

**Author:** ![gose](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gose/32/37875_2.png) [@gose](https://discuss.elastic.co/u/gose)\
**Post date:** [November 28, 2019, 1:15pm UTC](https://discuss.elastic.co/t/reference-result-of-aggregation-in-sub-aggregation/205425/3 "2019-11-28T13:15:13Z")

</div>

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