# Getting sum sub aggregation

**URL:** <https://discuss.elastic.co/t/getting-sum-sub-aggregation/93437>\
**Category:** Elasticsearch\
**Created:** [July 17, 2017, 5:42pm UTC](https://discuss.elastic.co/t/getting-sum-sub-aggregation/93437 "2017-07-17T17:42:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SamA](https://avatars.discourse-cdn.com/v4/letter/s/7993a0/32.png) [@SamA](https://discuss.elastic.co/u/SamA)\
**Post date:** [July 17, 2017, 5:42pm UTC](https://discuss.elastic.co/t/getting-sum-sub-aggregation/93437/1 "2017-07-17T17:42:16Z")

</div>

I'd like to get the sum of a sub aggregation. For example, I have group by smartphones, group by carrier and then the average price for that carrier. I'd like to get the sum of all prices for all carriers for a specific smartphone. So essentially, I want something like this:

```
{
  "aggs": {
    "group_by_smartphones": {
      "terms": {
        "field": "smartphone",
        "order": {
          "_term": "asc"
        },
        "size": 200
      },
      "aggs": {
        "group_by_sum": {
          "sum": {
            "field": "price"
          },
          "aggs": {
            "group_by_carrier": {
              "terms": {
                "field": "carrier",
                "order": {
                  "group_by_avg": "desc"
                }
              },
              "aggs": {
                "group_by_avg": {
                  "avg": {
                    "field": "price"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

```

Except, when I do it like this I get this error:

> "type": "aggregation\_initialization\_exception", "reason": "Aggregator [group\_by\_sum] of type [sum] cannot accept sub-aggregations"

How do I fix it so I can get the sum of all prices for each smartphone?

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [July 18, 2017, 8:26am UTC](https://discuss.elastic.co/t/getting-sum-sub-aggregation/93437/2 "2017-07-18T08:26:07Z")

</div>

Try the following. The problem with your request was that you were trying to do a sub aggregation under the sum aggregation which is not allowed. Metric aggregations like sum provide just a scalar output so cannot support sub-aggregations like bucket aggregations (which provide a bucket of documents) can.

You can, however have multiple aggregations at each level so what the below request is doing is asking for a terms aggregation by smartphone and then for each smartphone bucket created it is passing the documents in that bucket to both the sum aggregation (to get the sum of the price over all documents for that smartphone) and to the `group_by_carrier` terms aggregation (to get the average price of each carrier).

Hope that helps

```auto
{
  "aggs": {
    "group_by_smartphones": {
      "terms": {
        "field": "smartphone",
        "order": {
          "_term": "asc"
        },
        "size": 200
      },
      "aggs": {
        "group_by_sum": {
          "sum": {
            "field": "price"
          },
          "group_by_carrier": {
            "terms": {
              "field": "carrier",
              "order": {
                "group_by_avg": "desc"
              }
            },
            "aggs": {
              "group_by_avg": {
                "avg": {
                  "field": "price"
                }
              }
            }
          }
        }
      }
    }
  }
}

```

---

<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:** [August 15, 2017, 8:26am UTC](https://discuss.elastic.co/t/getting-sum-sub-aggregation/93437/3 "2017-08-15T08:26:09Z")

</div>

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