# How to calculate two aggregations on same field within the same body?

**URL:** <https://discuss.elastic.co/t/how-to-calculate-two-aggregations-on-same-field-within-the-same-body/193889>\
**Category:** Elasticsearch\
**Created:** [August 5, 2019, 9:50pm UTC](https://discuss.elastic.co/t/how-to-calculate-two-aggregations-on-same-field-within-the-same-body/193889 "2019-08-05T21:50:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kaushik123](https://avatars.discourse-cdn.com/v4/letter/k/87869e/32.png) [@Kaushik123](https://discuss.elastic.co/u/Kaushik123)\
**Post date:** [August 5, 2019, 9:50pm UTC](https://discuss.elastic.co/t/how-to-calculate-two-aggregations-on-same-field-within-the-same-body/193889/1 "2019-08-05T21:50:58Z")

</div>

I am trying to find the sum aggregate data(total count) of a field for my query. I also want to find the sum aggregate data of that same field, but on a different day/time. How can I write that has this type of functionality?

I have a bool query where I am specifying the range(date and hour) of the field I am aggregating on. Is it possible to do 2 aggs on the same field in one body?

---

<div class="post-metadata">

**Author:** ![gabriel\_tessier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gabriel_tessier/32/27911_2.png) [@gabriel\_tessier](https://discuss.elastic.co/u/gabriel_tessier)\
**Post date:** [August 6, 2019, 3:39am UTC](https://discuss.elastic.co/t/how-to-calculate-two-aggregations-on-same-field-within-the-same-body/193889/2 "2019-08-06T03:39:40Z")

</div>

Hi @Kaushik123,

Yes there's no problem to make 2 different aggregations on the same fields, as far as you call your aggregations with a different name so they'll not merge.

So far what did you try? can you provide some document example so I can help to build your aggregations.

---

<div class="post-metadata">

**Author:** ![kumar8055](https://avatars.discourse-cdn.com/v4/letter/k/278dde/32.png) [@kumar8055](https://discuss.elastic.co/u/kumar8055)\
**Post date:** [August 6, 2019, 4:35am UTC](https://discuss.elastic.co/t/how-to-calculate-two-aggregations-on-same-field-within-the-same-body/193889/3 "2019-08-06T04:35:35Z")

</div>

Hi Kaushik123,

It's not a problem to perform two aggregation on the same time. results will be two different columns. So, there would be no conflict.

Provide us the documents if you are facing in doing any aggregations.

---

<div class="post-metadata">

**Author:** ![Kaushik123](https://avatars.discourse-cdn.com/v4/letter/k/87869e/32.png) [@Kaushik123](https://discuss.elastic.co/u/Kaushik123)\
**Post date:** [August 6, 2019, 5:41pm UTC](https://discuss.elastic.co/t/how-to-calculate-two-aggregations-on-same-field-within-the-same-body/193889/4 "2019-08-06T17:41:26Z")

</div>

Hello @gabriel_tessier,

So far, I have 2 different bodies with 2 different aggregations.

[Queries for aggregations](https://discuss.elastic.co/t/comparing-the-result-sum-count-of-2-aggregations-elasticsearch/193826)

I linked another post shown above. What I am trying to do is compare the results(sum) of the 2 aggregations. In this case, I want to know what the 2xx count is today compared to yesterday. If the count is decreasing, I wish to print a warning or some sort.

How can I accomplish this using elasticsearch (elasticsearch dsl)?

---

<div class="post-metadata">

**Author:** ![Kaushik123](https://avatars.discourse-cdn.com/v4/letter/k/87869e/32.png) [@Kaushik123](https://discuss.elastic.co/u/Kaushik123)\
**Post date:** [August 6, 2019, 5:48pm UTC](https://discuss.elastic.co/t/how-to-calculate-two-aggregations-on-same-field-within-the-same-body/193889/5 "2019-08-06T17:48:16Z")

</div>

Thanks for the reply @kumar8055,

I did not have any trouble finding the aggregations. What I am trying to understand is how I can compare 2 aggregations, when both aggregations are performed on the same field, just different time/date.

The field I am calculating on is in a index called swift-xxx. So what I am doing is creating a body with the aggregation(date/time specified in a bool query), and then calling the result as shown below.

[Queries for the 2 aggregations](https://discuss.elastic.co/t/comparing-the-result-sum-count-of-2-aggregations-elasticsearch/193826)

count = es.search(index='swift-xxx', body=2xxCount)  
print(count)

count2 = es.search(index='swift-xxx', body=2xxLast2Days)  
print(count2)

Note that these two are 2 seperate queries that result 2 seperate sum aggregations. Is there any way I can combine then into one body and compare the result - i.e. see which 2xx is greater and print it.

---

<div class="post-metadata">

**Author:** ![gabriel\_tessier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gabriel_tessier/32/27911_2.png) [@gabriel\_tessier](https://discuss.elastic.co/u/gabriel_tessier)\
**Post date:** [August 7, 2019, 3:43am UTC](https://discuss.elastic.co/t/how-to-calculate-two-aggregations-on-same-field-within-the-same-body/193889/6 "2019-08-07T03:43:25Z")

</div>

Hi @Kaushik123,

I replied in the other thread, maybe better to keep only one thread to prevent duplication...  
As the other thread have the request code, it will be better to stop reply on this thread.  
🙇‍♂️

---

<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:** [September 4, 2019, 3:43am UTC](https://discuss.elastic.co/t/how-to-calculate-two-aggregations-on-same-field-within-the-same-body/193889/7 "2019-09-04T03:43:26Z")

</div>

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