# Count unique fields with sum parameter range

**URL:** <https://discuss.elastic.co/t/count-unique-fields-with-sum-parameter-range/145691>\
**Category:** Elasticsearch\
**Created:** [August 23, 2018, 9:35am UTC](https://discuss.elastic.co/t/count-unique-fields-with-sum-parameter-range/145691 "2018-08-23T09:35:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Smasell](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/smasell/32/43483_2.png) [@Smasell](https://discuss.elastic.co/u/Smasell)\
**Post date:** [August 23, 2018, 9:35am UTC](https://discuss.elastic.co/t/count-unique-fields-with-sum-parameter-range/145691/1 "2018-08-23T09:35:51Z")

</div>

Hi!!!  
I have my data points where I have two fields:  
user\_id  
buy\_price

Each user\_id can sent several data points(with different buy\_price value).  
Could you please help to count user\_id's, grouped by sum their buy\_prices(to 100, from 100 to 200, from 200).

Thanks for helping me!

---

<div class="post-metadata">

**Author:** ![mayya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayya/32/83147_2.png) [@mayya](https://discuss.elastic.co/u/mayya)\
**Post date:** [August 31, 2018, 8:44pm UTC](https://discuss.elastic.co/t/count-unique-fields-with-sum-parameter-range/145691/2 "2018-08-31T20:44:59Z")

</div>

If I understood your problem correctly, you wanted to have buckets based on `buy_price` and for each bucket count of `user_id` for this price.  
[Range aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-range-aggregation.html) should help you here.

```auto
GET /_search
{
    "aggs" : {
        "price_ranges" : {
            "range" : {
                "field" : "price",
                "ranges" : [
                    { "to" : 100.0 },
                    { "from" : 100.0, "to" : 200.0 },
                    { "from" : 200.0 }
                ]
            }
        }
    }
}

```

If this is not what you intended, please provide an example for the response you are looking for.

---

<div class="post-metadata">

**Author:** ![Smasell](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/smasell/32/43483_2.png) [@Smasell](https://discuss.elastic.co/u/Smasell)\
**Post date:** [September 5, 2018, 7:38am UTC](https://discuss.elastic.co/t/count-unique-fields-with-sum-parameter-range/145691/3 "2018-09-05T07:38:11Z")

</div>

Thank's for your response!  
Everything right, but it's not what I'm looking for. For example I have several docs:

user\_id: Ben buy\_price: 50  
user\_id: Ben buy\_price: 100  
user\_id: Ben buy\_price: 100  
user\_id: Jane buy\_price: 50  
user\_id: Luke buy\_price: 250

I need to count sum of their buy\_price fields and count how many unique user\_id each sum range has.  
Response in this case should look like this:

to 100 - 1 (Jane. because sum of all her buys is equal 50)  
from 100 to 200 - 0  
from 200 - 2(Luke has one doc with price 250 and Ben has 3 docs (100+100+50))

Thanks for helping me.

---

<div class="post-metadata">

**Author:** ![mayya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayya/32/83147_2.png) [@mayya](https://discuss.elastic.co/u/mayya)\
**Post date:** [September 7, 2018, 9:21pm UTC](https://discuss.elastic.co/t/count-unique-fields-with-sum-parameter-range/145691/4 "2018-09-07T21:21:00Z")

</div>

I understand now what you intended to do. Unfortunately, you can not do this in elasticsearch.  
This would involve having a pipeline range aggregation, which we have decided not to develop. You can read more here [https://github.com/elastic/elasticsearch/issues/17590](https://github.com/elastic/elasticsearch/issues/17590)

---

<div class="post-metadata">

**Author:** ![Smasell](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/smasell/32/43483_2.png) [@Smasell](https://discuss.elastic.co/u/Smasell)\
**Post date:** [September 8, 2018, 12:22pm UTC](https://discuss.elastic.co/t/count-unique-fields-with-sum-parameter-range/145691/5 "2018-09-08T12:22:36Z")

</div>

Get it! Mayya, many thanks and good luck!!!)

---

<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:** [October 6, 2018, 12:22pm UTC](https://discuss.elastic.co/t/count-unique-fields-with-sum-parameter-range/145691/6 "2018-10-06T12:22:42Z")

</div>

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