# How to generate a sum of averages graph

**URL:** <https://discuss.elastic.co/t/how-to-generate-a-sum-of-averages-graph/364675>\
**Category:** Kibana\
**Created:** [August 9, 2024, 1:26pm UTC](https://discuss.elastic.co/t/how-to-generate-a-sum-of-averages-graph/364675 "2024-08-09T13:26:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![francieliton\_araujo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/francieliton_araujo/32/47771_2.png) [@francieliton\_araujo](https://discuss.elastic.co/u/francieliton_araujo)\
**Post date:** [August 9, 2024, 1:26pm UTC](https://discuss.elastic.co/t/how-to-generate-a-sum-of-averages-graph/364675/1 "2024-08-09T13:26:57Z")

</div>

Could someone help me? I don't even know where to start. I have the following data:  
location | quantity |  
A | 20 |  
B | 35 |  
C | 32 |  
B | 15 |  
A | 40 |  
C | 8 |  
I would like the result to be the average per location and then add everything up.  
A =\> (20+40)/2 = 30  
B =\> (35+15)/2 = 25  
C =\> (32+8)/2 = 20  
TOTAL = 75

I don't care what kind of "visualize Library" I just need to understand how to group it like this

---

<div class="post-metadata">

**Author:** ![ashishtiwari1993](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ashishtiwari1993/32/135241_2.png) [@ashishtiwari1993](https://discuss.elastic.co/u/ashishtiwari1993)\
**Post date:** [August 12, 2024, 1:45pm UTC](https://discuss.elastic.co/t/how-to-generate-a-sum-of-averages-graph/364675/2 "2024-08-12T13:45:27Z")

</div>

Hi @francieliton_araujo,

You can go to dev tools and perform below poc:

**Create an Index**

```auto
PUT calc-avg
{
  "mappings": {
    "properties": {
      "location": {
        "type": "keyword"
      },
      "quantity": {
        "type": "long"
      }
    }
  }
}

```

**Insert documents**

```auto
POST calc-avg/_doc
{
  "location": "A",
  "quantity": 20
}

POST calc-avg/_doc
{
  "location": "B",
  "quantity": 35
}

POST calc-avg/_doc
{
  "location": "C",
  "quantity": 32
}

POST calc-avg/_doc
{
  "location": "B",
  "quantity": 15
}

POST calc-avg/_doc
{
  "location": "A",
  "quantity": 40
}

POST calc-avg/_doc
{
  "location": "C",
  "quantity": 8
}

```

**Calculate avg with sum using** [pipeline aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline.html)

```auto
GET calc-avg/_search?size=0
{
  "aggs": {
    "avg_location": {
      "avg": {
        "field": "quantity"
      }
    }
  }
}

GET calc-avg/_search?size=0
{
  "aggs": {
    "terms_location": {
      "terms": {
        "field": "location"
      },
      "aggs": {
        "avg_location": {
          "avg": {
            "field": "quantity"
          }
        }
      }
    },
    "total": {
      "sum_bucket": {
        "buckets_path": "terms_location>avg_location"
      }
    }
  }
}

```

**Response**

```auto
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 6,
      "relation": "eq"
    },
    "max_score": null,
    "hits": []
  },
  "aggregations": {
    "terms_location": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "A",
          "doc_count": 2,
          "avg_location": {
            "value": 30
          }
        },
        {
          "key": "B",
          "doc_count": 2,
          "avg_location": {
            "value": 25
          }
        },
        {
          "key": "C",
          "doc_count": 2,
          "avg_location": {
            "value": 20
          }
        }
      ]
    },
    "total": {
      "value": 75
    }
  }
}

```
