# Group By and Sum the values in each group

**URL:** <https://discuss.elastic.co/t/group-by-and-sum-the-values-in-each-group/58517>\
**Category:** Kibana\
**Created:** [August 20, 2016, 11:25pm UTC](https://discuss.elastic.co/t/group-by-and-sum-the-values-in-each-group/58517 "2016-08-20T23:25:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thirunavukkarasu\_Rav](https://avatars.discourse-cdn.com/v4/letter/t/c77e96/32.png) [@Thirunavukkarasu\_Rav](https://discuss.elastic.co/u/Thirunavukkarasu_Rav)\
**Post date:** [August 20, 2016, 11:25pm UTC](https://discuss.elastic.co/t/group-by-and-sum-the-values-in-each-group/58517/1 "2016-08-20T23:25:21Z")

</div>

Hi,

I need get the sum of each item quantity? My data:

```
{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 21,
    "successful": 21,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1,
    "hits": [
      {
        "_index": "bill",
        "_type": "bill",
        "_id": "2650",
        "_score": 1,
        "_source": {
          "id": 2650,
          "name": "Order 1",
          "orders": [
            {
              "id": 3200,
              "item": {
                "id": 3600,
                "name": "Barbeque",
                "price": 175
              },
              "quantity": 1,
              "created": "2016-08-20 21:01:09"
            },
            {
              "id": 3201,
              "item": {
                "id": 3601,
                "name": "Pepsi",
                "price": 25,
                },
              "quantity": 1,
              "created": "2016-08-20 21:01:15"
            },
            {
              "id": 3202,
              "item": {
                "id": 3602,
                "name": "Beer",
                "price": 120
              },
              "quantity": 1,
              "created": "2016-08-20 21:01:20"
            }
          ]
        }
      },
      {
        "_index": "bill",
        "_type": "bill",
        "_id": "3000",
        "_score": 1,
        "_source": {
          "id": 3000,
          "name": "Order 2",
          "orders": [
            {
              "id": 3203,
              "item": {
                "id": 3602,
                "name": "Beer",
                "price": 120
              },
              "quantity": 2,
              "created": "2016-08-20 15:34:09"
            },
            {
              "id": 3204,
              "item": {
                "id": 3601,
                "name": "Pepsi",
                "price": 25
              },
              "quantity": 1,
              "created": "2016-08-20 21:04:23"
            }
          ]
        }
      }
    ]
  }
}

```

The output should be like:  
Item name Quantity  
Pepsi 2  
Beer 3  
Barbeque 1

Can you help me get this data table in kibana?

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [August 22, 2016, 6:30am UTC](https://discuss.elastic.co/t/group-by-and-sum-the-values-in-each-group/58517/2 "2016-08-22T06:30:22Z")

</div>

Please format your code with the `</>` button, it’s very hard to read.

---

<div class="post-metadata">

**Author:** ![cjcenizal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cjcenizal/32/11216_2.png) [@cjcenizal](https://discuss.elastic.co/u/cjcenizal)\
**Post date:** [August 22, 2016, 6:00pm UTC](https://discuss.elastic.co/t/group-by-and-sum-the-values-in-each-group/58517/3 "2016-08-22T18:00:33Z")

</div>

Hi there,

After you create a "Data Table" visualization, I think you'll need to do these two steps:

1. Twirl down the Metric aggregation and change it from "Count" to "Sum". Change the field to "quantity".
2. Beneath "buckets", select the "Split Rows" bucket type. Choose a "Terms" aggregation and set the field to "[item.name](http://item.name)".

When you hit the play button to create the visualization, you should see the data table you're looking for.

Thanks,  
CJ

---

<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:** [July 6, 2017, 1:40pm UTC](https://discuss.elastic.co/t/group-by-and-sum-the-values-in-each-group/58517/4 "2017-07-06T13:40:36Z")

</div>


