# About aggregates the results of buckets twice

**URL:** <https://discuss.elastic.co/t/about-aggregates-the-results-of-buckets-twice/189304>\
**Category:** Elasticsearch\
**Created:** [July 8, 2019, 8:09am UTC](https://discuss.elastic.co/t/about-aggregates-the-results-of-buckets-twice/189304 "2019-07-08T08:09:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![xuziqi](https://avatars.discourse-cdn.com/v4/letter/x/b5ac83/32.png) [@xuziqi](https://discuss.elastic.co/u/xuziqi)\
**Post date:** [July 8, 2019, 8:09am UTC](https://discuss.elastic.co/t/about-aggregates-the-results-of-buckets-twice/189304/1 "2019-07-08T08:09:36Z")

</div>

hello，  
Elastic 6.3.0

Now，We hive a table of Consumption details.(A person has multiple consumption records ).

First ,we want to Calculate a person's total consumption（need to agg）.

Then we want to get all the people who consume more than $500.（It's the total, not the details.）.

So we need to aggregate twice on the basis of calculating a person's total consumption amount.

Like the sql:  
select count(1)  
from (select \*  
from (select mobile\_nbr, sum(trans\_amt) total\_trans\_amt  
from activity\_detail  
group by mobile\_nbr) t  
where t.total\_trans\_amt \> 500) tt

Excuse me, do es support this query?

best wish to you  
thanks

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [July 9, 2019, 9:25am UTC](https://discuss.elastic.co/t/about-aggregates-the-results-of-buckets-twice/189304/2 "2019-07-09T09:25:14Z")

</div>

It sounds like you're looking for the [bucket selector aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/6.3/search-aggregations-pipeline-bucket-selector-aggregation.html). The docs have an example that's very similar to what you're trying to do.

---

<div class="post-metadata">

**Author:** ![xuziqi](https://avatars.discourse-cdn.com/v4/letter/x/b5ac83/32.png) [@xuziqi](https://discuss.elastic.co/u/xuziqi)\
**Post date:** [July 9, 2019, 9:37am UTC](https://discuss.elastic.co/t/about-aggregates-the-results-of-buckets-twice/189304/4 "2019-07-09T09:37:23Z")

</div>

@abdon  
hello，thanks for your answer.  
I had tried the bucket selector aggregation.But the result is the details are not the total.  
I want to get the number of people that is not people's details.  
Can we get the result~

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [July 9, 2019, 9:39am UTC](https://discuss.elastic.co/t/about-aggregates-the-results-of-buckets-twice/189304/5 "2019-07-09T09:39:57Z")

</div>

I think you would have to do that client-side: count the number of buckets that the Bucket Selector aggregation returns.

---

<div class="post-metadata">

**Author:** ![xuziqi](https://avatars.discourse-cdn.com/v4/letter/x/b5ac83/32.png) [@xuziqi](https://discuss.elastic.co/u/xuziqi)\
**Post date:** [July 9, 2019, 9:42am UTC](https://discuss.elastic.co/t/about-aggregates-the-results-of-buckets-twice/189304/6 "2019-07-09T09:42:38Z")

</div>

@abdon  
ok,I see.  
thank you very much

---

<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:** [August 6, 2019, 9:42am UTC](https://discuss.elastic.co/t/about-aggregates-the-results-of-buckets-twice/189304/7 "2019-08-06T09:42:43Z")

</div>

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