# Sum of unique ids

**URL:** <https://discuss.elastic.co/t/sum-of-unique-ids/134065>\
**Category:** Elasticsearch\
**Created:** [May 31, 2018, 1:44pm UTC](https://discuss.elastic.co/t/sum-of-unique-ids/134065 "2018-05-31T13:44:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Islam\_Elshobokshy](https://avatars.discourse-cdn.com/v4/letter/i/77aa72/32.png) [@Islam\_Elshobokshy](https://discuss.elastic.co/u/Islam_Elshobokshy)\
**Post date:** [May 31, 2018, 1:44pm UTC](https://discuss.elastic.co/t/sum-of-unique-ids/134065/1 "2018-05-31T13:44:38Z")

</div>

I have an aggregation that sums the column quantity, but I want it to sum the column quantity of unique IDS. Knowing that each unique id, has a unique quantity value.

For example, if I have :

> id = 5 | quantity = 5  
> id = 5 | quantity = 5  
> id = 5 | quantity = 5  
> id = 6 | quantity = 3  
> id = 6 | quantity = 3

I want the sum to count this :

> id = 5 | quantity = 5  
> id = 6 | quantity = 3

Which is 8.

This sum aggregation is used as a sub aggregation of another Terms aggregation which I won't bother explaining here as if I get the sum agg to work the other one is easy, I think. How to do that?

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [May 31, 2018, 2:17pm UTC](https://discuss.elastic.co/t/sum-of-unique-ids/134065/2 "2018-05-31T14:17:25Z")

</div>

Do the IDs and quantities always stay constant? E.g. id 6 always has quantity 3? If a single ID can have different quantities, which one do you pick for the calculation?

I have some ideas how to solve this but didn't want to get too deep in the weeds before getting more info 🙂

---

<div class="post-metadata">

**Author:** ![Islam\_Elshobokshy](https://avatars.discourse-cdn.com/v4/letter/i/77aa72/32.png) [@Islam\_Elshobokshy](https://discuss.elastic.co/u/Islam_Elshobokshy)\
**Post date:** [May 31, 2018, 2:19pm UTC](https://discuss.elastic.co/t/sum-of-unique-ids/134065/3 "2018-05-31T14:19:07Z")

</div>

They don't always stay constant, but for each unique id there's a unique quantity. As shown in my example, if there are several unique ids they'll have the same unique quantity. So actually yes, they're constant in a way. Please do help 😃

> E.g. id 6 always has quantity 3

Yes. Or always has quantity 5, or 10 or whatever, but they always have the same exact quantity if they are all id 6.

---

<div class="post-metadata">

**Author:** ![Islam\_Elshobokshy](https://avatars.discourse-cdn.com/v4/letter/i/77aa72/32.png) [@Islam\_Elshobokshy](https://discuss.elastic.co/u/Islam_Elshobokshy)\
**Post date:** [June 1, 2018, 7:01am UTC](https://discuss.elastic.co/t/sum-of-unique-ids/134065/4 "2018-06-01T07:01:42Z")

</div>

@polyfractal so any idea? Sorry to be persistant 😃

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [June 4, 2018, 2:04pm UTC](https://discuss.elastic.co/t/sum-of-unique-ids/134065/5 "2018-06-04T14:04:48Z")

</div>

Ok cool, that makes things easier.

What I would do is:

- `terms` aggregation on ID
  - `max` metric aggregation on quantity. Because all the quantities will be the same, a `max` (or `min`) will just return the value.
  - `sum_bucket` pipeline aggregation to add up all the quantities. Pipeline aggregations act on the result of other aggs, so this `sum_bucket` will point to the `max` metric and sum them all up.

---

<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 2, 2018, 2:04pm UTC](https://discuss.elastic.co/t/sum-of-unique-ids/134065/6 "2018-07-02T14:04:51Z")

</div>

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