# Adding new value after calculation in Aggregation

**URL:** <https://discuss.elastic.co/t/adding-new-value-after-calculation-in-aggregation/26814>\
**Category:** Elasticsearch\
**Created:** [August 4, 2015, 3:29pm UTC](https://discuss.elastic.co/t/adding-new-value-after-calculation-in-aggregation/26814 "2015-08-04T15:29:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aditya\_Patel](https://avatars.discourse-cdn.com/v4/letter/a/3bc359/32.png) [@Aditya\_Patel](https://discuss.elastic.co/u/Aditya_Patel)\
**Post date:** [August 4, 2015, 3:29pm UTC](https://discuss.elastic.co/t/adding-new-value-after-calculation-in-aggregation/26814/1 "2015-08-04T15:29:03Z")

</div>

Hi

Currently i am trying to group a field based on one field and than getting sum of other fields with respect to the respective field used for grouping. I want to get a new value which needs to be division of the summed field . I will provide the current query i have :

**Query1:**

GET /index1/table1/\_search  
{  
"size": 0,  
"query": {  
"filtered": {  
"query": {  
"query\_string": {  
"query": "\*",  
"analyze\_wildcard": true  
}  
},  
"filter": {  
"bool": {  
"must": [  
{  
"range": {  
"start\_timestamp": {  
"gte": 1430483327935,  
"lte": 1432988927935  
}  
}  
}  
],  
"must": [  
{"term": {  
"account\_id": 29  
}  
}  
],  
"must\_not": []  
}  
}  
}},  
"aggs": {  
"custom\_name": {  
"terms": {  
"field": "a\_name"  
},  
"aggs": {  
"spe": {  
"sum": {  
"field": "spend"  
}  
},"gained": {  
"sum": {  
"field": "gain"  
}  
}  
}  
}  
}  
}

Now in my query i am aggregating them based on the field ("a\_name") and summing "spend" and "gain". I want to get a new field which would be ratio of sum (spend/gain)

\*\*

## Basically I would like to divide "spe" and "gained" and get another field "ratio"

\*\*

I also tried adding script but i am getting NaN , also to enable this; i had to enable them first in elasticsearch.yml file

script.engine.groovy.inline.aggs: on

**Query2**

GET /index1/table1/\_search  
{  
"size": 0,  
"query": {  
"filtered": {  
"query": {  
"query\_string": {  
"query": "\*",  
"analyze\_wildcard": true  
}  
},  
"filter": {  
"bool": {  
"must": [  
{  
"range": {  
"start\_timestamp": {  
"gte": 1430483327935,  
"lte": 1432988927935  
}  
}  
}  
],  
"must": [  
{"term": {  
"account\_id": 29  
}  
}  
],  
"must\_not": []  
}  
}  
}},  
"aggs": {  
"custom\_name": {  
"terms": {  
"field": "a\_name"  
},  
"aggs": {  
"spe": {  
"sum": {  
"field": "spend"  
}  
},"gained": {  
"sum": {  
"field": "gain"  
}  
},  
"rati":{  
"sum":{ "script":"doc['spend'].value/doc['gain'].value"  
}  
}  
}  
}  
}  
}

This particular query is showing me a 'NaN' in output.

Thanks!

---

<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 5, 2017, 11:57pm UTC](https://discuss.elastic.co/t/adding-new-value-after-calculation-in-aggregation/26814/2 "2017-07-05T23:57:29Z")

</div>


