# Finding sum of average sub aggregations Ask Question

**URL:** <https://discuss.elastic.co/t/finding-sum-of-average-sub-aggregations-ask-question/94249>\
**Category:** Elasticsearch\
**Created:** [July 23, 2017, 5:47pm UTC](https://discuss.elastic.co/t/finding-sum-of-average-sub-aggregations-ask-question/94249 "2017-07-23T17:47:27Z")\
**Posts on this page:** 4\
**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 23, 2017, 5:47pm UTC](https://discuss.elastic.co/t/finding-sum-of-average-sub-aggregations-ask-question/94249/1 "2017-07-23T17:47:27Z")

</div>

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

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

```

When I try doing this query, I get an error saying:

> "type": "parsing\_exception", "reason": "Unexpected token VALUE\_STRING [field] in [group\_by\_sum]",

So essentially I want to get the sum of the averages of all carriers for a particular smartphone. Is there a way to get this?

---

<div class="post-metadata">

**Author:** ![PaulieMac](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pauliemac/32/19480_2.png) [@PaulieMac](https://discuss.elastic.co/u/PaulieMac)\
**Post date:** [July 23, 2017, 7:11pm UTC](https://discuss.elastic.co/t/finding-sum-of-average-sub-aggregations-ask-question/94249/2 "2017-07-23T19:11:09Z")

</div>

Looks like your 'group\_by\_sum' agg is already a child of 'group\_by\_smartphones'... Also, the sum\_bucket expects 'buckets\_path' rather than 'field'... So your pipeline sum can just be:

```auto
"sum_bucket": {
            "buckets_path": "group_by_carrier>group_by_avg"
          }

```

---

<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 23, 2017, 7:34pm UTC](https://discuss.elastic.co/t/finding-sum-of-average-sub-aggregations-ask-question/94249/4 "2017-07-23T19:34:40Z")

</div>

Could you please give me the full query because i'm still getting an error. The error is:

> Unexpected token VALUE\_STRING [field] in [group\_by\_sum]

---

<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 20, 2017, 7:35pm UTC](https://discuss.elastic.co/t/finding-sum-of-average-sub-aggregations-ask-question/94249/5 "2017-08-20T19:35:14Z")

</div>

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