# How to generate a sum of averages graph

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

You can go to dev tools and perform below poc:

Create an Index

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

Insert documents

``````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

``````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

``````{
"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
}
}
}
``````