# Sum of a field after terms aggregation

**URL:** <https://discuss.elastic.co/t/sum-of-a-field-after-terms-aggregation/325985>\
**Category:** Elasticsearch\
**Created:** [February 20, 2023, 2:46pm UTC](https://discuss.elastic.co/t/sum-of-a-field-after-terms-aggregation/325985 "2023-02-20T14:46:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Miguel\_Azorin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/miguel_azorin/32/99030_2.png) [@Miguel\_Azorin](https://discuss.elastic.co/u/Miguel_Azorin)\
**Post date:** [February 20, 2023, 2:46pm UTC](https://discuss.elastic.co/t/sum-of-a-field-after-terms-aggregation/325985/1 "2023-02-20T14:46:24Z")

</div>

Hi!

Given docs as the ones in the example below:

```auto
{ "id" : "1", "numValue": 9.7 }
{ "id" : "1", "numValue": 9.7 }
{ "id" : "1", "numValue": 9.7 }
{ "id" : "2", "numValue": 7 }
{ "id" : "2", "numValue": 7 }
{ "id" : "3", "numValue": 10 }

```

I'm trying to create a query that groups the docs by id like this (a simple terms aggregation):

```auto
{ "id" : "1", "numValue": 9.7 }
{ "id" : "2", "numValue": 7 }
{ "id" : "3", "numValue": 10 }

```

And then sums the "numValue": in this example 9.7 + 7 + 10.

I know I can get the first part done by using a max aggregation inside a terms, but I don't know how to get the total sum:

```auto
  "aggs": {
    "group_by_ID": {
      "terms": {
        "field": "ID",
        "min_doc_count": 1
      },
      "aggs": {
        "maxValue": {
          "max": {
            "field": "numvalue"
          }
        }
      }
    }
  }

```

I've also tried to add a sum aggregation that sums "maxValue", but the result is always 0.

A visualization would be a good option too, if it's easier that way, but it would need be compatible with Kibana 7.9.2.

Any help would be appreciated!

Kind regards,

Miguel

---

<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:** [February 20, 2023, 2:46pm UTC](https://discuss.elastic.co/t/sum-of-a-field-after-terms-aggregation/325985/2 "2023-02-20T14:46:24Z")

</div>

Kibana 7.9 is [EOL](https://www.elastic.co/support/eol) and no longer supported. Please upgrade ASAP.

(This is an automated response from your friendly Elastic bot. Please report this post if you have any suggestions or concerns :elasticheart: )

---

<div class="post-metadata">

**Author:** ![Miguel\_Azorin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/miguel_azorin/32/99030_2.png) [@Miguel\_Azorin](https://discuss.elastic.co/u/Miguel_Azorin)\
**Post date:** [February 20, 2023, 2:51pm UTC](https://discuss.elastic.co/t/sum-of-a-field-after-terms-aggregation/325985/3 "2023-02-20T14:51:00Z")

</div>

Ok, then I would accept a solution even if it's not compatible with Kibana 7.9.2 hahaha

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [February 20, 2023, 5:28pm UTC](https://discuss.elastic.co/t/sum-of-a-field-after-terms-aggregation/325985/4 "2023-02-20T17:28:18Z")

</div>

Hi @Miguel_Azorin

Try use [sum buckets](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-sum-bucket-aggregation.html):

```auto
{
  "size": 0,
  "aggs": {
    "group_by_ID": {
      "terms": {
        "field": "id",
        "min_doc_count": 1
      },
      "aggs": {
        "maxValue": {
          "max": {
            "field": "numValue"
          }
        }
      }
    },
    "total": {
      "sum_bucket": {
        "buckets_path": "group_by_ID>maxValue"
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Miguel\_Azorin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/miguel_azorin/32/99030_2.png) [@Miguel\_Azorin](https://discuss.elastic.co/u/Miguel_Azorin)\
**Post date:** [February 21, 2023, 8:26am UTC](https://discuss.elastic.co/t/sum-of-a-field-after-terms-aggregation/325985/5 "2023-02-21T08:26:32Z")

</div>

> [@RabBit\_BR](#):
>
> `sum_bucket`

Hi!

It works like a charm! Thank you very much!

Best regards,

Miguel

---

<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:** [March 21, 2023, 8:26am UTC](https://discuss.elastic.co/t/sum-of-a-field-after-terms-aggregation/325985/6 "2023-03-21T08:26:34Z")

</div>

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