# Sum greater than some amount group by Phone

**URL:** <https://discuss.elastic.co/t/sum-greater-than-some-amount-group-by-phone/153325>\
**Category:** Elasticsearch\
**Created:** [October 22, 2018, 6:30am UTC](https://discuss.elastic.co/t/sum-greater-than-some-amount-group-by-phone/153325 "2018-10-22T06:30:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nikhil\_Nambiar](https://avatars.discourse-cdn.com/v4/letter/n/9fc29f/32.png) [@Nikhil\_Nambiar](https://discuss.elastic.co/u/Nikhil_Nambiar)\
**Post date:** [October 22, 2018, 6:30am UTC](https://discuss.elastic.co/t/sum-greater-than-some-amount-group-by-phone/153325/1 "2018-10-22T06:30:12Z")

</div>

I have a json

{  
phone : "phone numbers"  
amount :"amount of call"  
uts:"date in text"  
}

how can I achieve a query which will give me sum of amount for individual phone and filter out those phone whose sum of amount is greater than some amount

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [October 22, 2018, 8:41am UTC](https://discuss.elastic.co/t/sum-greater-than-some-amount-group-by-phone/153325/2 "2018-10-22T08:41:18Z")

</div>

you can use a [range query](https://www.elastic.co/guide/en/elasticsearch/reference/6.4/query-dsl-range-query.html) to filter to an amount and then use the [sum aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/6.4/search-aggregations-metrics-sum-aggregation.html)

Note that in case of the above being within an array, you need to take a look at the [nested datatype](https://www.elastic.co/guide/en/elasticsearch/reference/6.4/nested.html)

---

<div class="post-metadata">

**Author:** ![Nikhil\_Nambiar](https://avatars.discourse-cdn.com/v4/letter/n/9fc29f/32.png) [@Nikhil\_Nambiar](https://discuss.elastic.co/u/Nikhil_Nambiar)\
**Post date:** [October 23, 2018, 3:35am UTC](https://discuss.elastic.co/t/sum-greater-than-some-amount-group-by-phone/153325/3 "2018-10-23T03:35:07Z")

</div>

Can anyone help me, where I am making mistake. My target is to get sum of amount respectively to phone number between the specified date range

```
{

"query":{
	"range":{
    	"uts":{
          "gte":"2018-08-01",
          "lte":"2018-08-31",
          "format":"yyyy-MM-dd"
        }
    }
},
"aggs":{
  "group_by_phone":{
	"filter":{
  "terms":{
      "field":"phone.keyword"
    }
 }
},
"aggs":{
	"total_sum":{
    	"sum":{"field":"amount"}
       }
    }
  }

}
```

---

<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:** [November 20, 2018, 3:35am UTC](https://discuss.elastic.co/t/sum-greater-than-some-amount-group-by-phone/153325/4 "2018-11-20T03:35:10Z")

</div>

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