# How to aggregate and sum the data grouped by some fields

**URL:** <https://discuss.elastic.co/t/how-to-aggregate-and-sum-the-data-grouped-by-some-fields/215207>\
**Category:** Elasticsearch\
**Created:** [January 15, 2020, 8:54pm UTC](https://discuss.elastic.co/t/how-to-aggregate-and-sum-the-data-grouped-by-some-fields/215207 "2020-01-15T20:54:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mihir\_Kothari](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mihir_kothari/32/46467_2.png) [@Mihir\_Kothari](https://discuss.elastic.co/u/Mihir_Kothari)\
**Post date:** [January 15, 2020, 8:54pm UTC](https://discuss.elastic.co/t/how-to-aggregate-and-sum-the-data-grouped-by-some-fields/215207/1 "2020-01-15T20:54:24Z")

</div>

Based on below 2 messages, I need to aggregate the data based on:

1. granularity (MyHeader.granularity)
2. myType (MyHeader.myType)
3. Currency.

> {
> 
> ```
> "_source": {
> "MyData": {
> "MyHeader": {
> "granularity": "Level1",
> "myType": "YTD",
> "businessDate": "2018-12-27"
> },
> "MyBreakDown": [
> {
> "category": null,
> "Currency": "eur",
> "MyDetails": [
> {
> "Id": 123,
> "myLocalAmount": 100
> }
> ]
> },
> {
> "category": null,
> "Currency": "clf",
> "MyDetails": [
> {
> "Id": 234,
> "myLocalAmount": 130
> }
> ]
> },
> {
> "category": null,
> "Currency": "usd",
> "MyDetails": [
> {
> "Id": 120,
> "myLocalAmount": 250
> }
> ]
> }
> ]
> }
> }
> },
> {
> 
> "_source": {
> "MyData": {
> "MyHeader": {
> "granularity": "Level1",
> "myType": "MTD",
> "businessDate": "2018-12-27"
> },
> "MyBreakDown": [
> {
> "category": null,
> "Currency": "eur",
> "MyDetails": [
> {
> "Id": 123,
> "myLocalAmount": 110
> }
> ]
> },
> {
> "category": null,
> "Currency": "clf",
> "MyDetails": [
> {
> "Id": 234,
> "myLocalAmount": 120
> }
> ]
> },
> {
> "category": null,
> "Currency": "usd",
> "MyDetails": [
> {
> "Id": 120,
> "myLocalAmount": 253
> }
> ]
> }
> ]
> }
> }
> }
> 
> ```

Here I can do basic aggregation:

> "aggs": {  
> "by\_granularity": {  
> "terms" : {  
> "field": "MyHeader.granularity"  
> },  
> "aggs": {  
> "by\_myType ": {  
> "terms": {  
> "field": "MyHeader.myType"  
> }  
> }  
> }  
> }  
> },  
> "size":0

However, Currency is under the array field and unable to use it.

I need help how to also use currency in aggregations, so it will give me correct data in buckets.

Data is expected something like:

Level1-\>YTD-\>eur = 100  
Level1-\>YTD-\>clf= 130  
Level1-\>YTD-\>usd = 250

Level1-\>MTD-\>eur = 110  
Level1-\>MTD-\>clf = 120  
Level1-\>MTD-\>usd = 253

---

<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:** [February 12, 2020, 8:54pm UTC](https://discuss.elastic.co/t/how-to-aggregate-and-sum-the-data-grouped-by-some-fields/215207/2 "2020-02-12T20:54:25Z")

</div>

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