# How to do the sum aggregation based on unique id

**URL:** <https://discuss.elastic.co/t/how-to-do-the-sum-aggregation-based-on-unique-id/155139>\
**Category:** Kibana\
**Created:** [November 2, 2018, 9:35am UTC](https://discuss.elastic.co/t/how-to-do-the-sum-aggregation-based-on-unique-id/155139 "2018-11-02T09:35:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nethaji](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nethaji/32/72803_2.png) [@Nethaji](https://discuss.elastic.co/u/Nethaji)\
**Post date:** [November 2, 2018, 9:35am UTC](https://discuss.elastic.co/t/how-to-do-the-sum-aggregation-based-on-unique-id/155139/1 "2018-11-02T09:35:51Z")

</div>

Hi ELK Folks,

I have a doubt on sum aggregation,

My sample data contains three columns

id status\_id cost  
1 1 1000  
1 5 1000  
1 6 1000  
2 1 2000  
3 1 3000  
3 5 3000  
4 6 1000

I'm having data like this , if i do the sum aggregation for cost means it shows 12000 i.e., it calculate the total value.

I don't want like this, i need the sum aggregation based on unique id, i.e., my expected result is 7000

here I couldn't able to get the correct output

please provide some solution to achieve the correct output.

---

<div class="post-metadata">

**Author:** ![nickpeihl](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nickpeihl/32/112622_2.png) [@nickpeihl](https://discuss.elastic.co/u/nickpeihl)\
**Post date:** [November 2, 2018, 9:44pm UTC](https://discuss.elastic.co/t/how-to-do-the-sum-aggregation-based-on-unique-id/155139/2 "2018-11-02T21:44:47Z")

</div>

Hi Nethanji,

You are looking for a [terms aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html). In your case it might look like this.

```auto
GET _search/
{
  "aggs": {
    "total_cost": {
      "terms": {
        "field": "id",
        "size": 4
      },
      "aggs": {
        "cost": {
          "sum": {
            "field": "cost"
          }
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Nethaji](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nethaji/32/72803_2.png) [@Nethaji](https://discuss.elastic.co/u/Nethaji)\
**Post date:** [November 8, 2018, 8:36am UTC](https://discuss.elastic.co/t/how-to-do-the-sum-aggregation-based-on-unique-id/155139/3 "2018-11-08T08:36:38Z")

</div>

Hi,  
Thank you for your reply

I need to show this in gauge report.

---

<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:** [December 6, 2018, 8:36am UTC](https://discuss.elastic.co/t/how-to-do-the-sum-aggregation-based-on-unique-id/155139/4 "2018-12-06T08:36:53Z")

</div>

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