# Cumulative sum query inside nested term aggregation \> avg aggregation

**URL:** <https://discuss.elastic.co/t/cumulative-sum-query-inside-nested-term-aggregation-avg-aggregation/182276>\
**Category:** Elasticsearch\
**Created:** [May 22, 2019, 3:57pm UTC](https://discuss.elastic.co/t/cumulative-sum-query-inside-nested-term-aggregation-avg-aggregation/182276 "2019-05-22T15:57:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vinay\_srinivas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vinay_srinivas/32/46648_2.png) [@vinay\_srinivas](https://discuss.elastic.co/u/vinay_srinivas)\
**Post date:** [May 22, 2019, 3:57pm UTC](https://discuss.elastic.co/t/cumulative-sum-query-inside-nested-term-aggregation-avg-aggregation/182276/1 "2019-05-22T15:57:20Z")

</div>

```
Hi,

```

I want to calculate cumulative sum inside nested term aggregation with avg aggregation.

Document:  
{  
"\_index" : "storagemetric",  
"\_type" : "metric",  
"\_id" : "OwjM32oBFWuNUT6ZDzMS",  
"\_score" : 1.0,  
"\_source" : {  
"subscriberId" : 22222,  
"createdTimestamp" : 1555976400000,  
"name" : "zzz",  
"metricId" : null,  
"accountID" : "326952836038",  
"cloudType" : "aws",  
"attributes" : {  
"all\_bssize\_average\_bytes" : 11864.0,  
"numberofobjects\_average\_count" : 3.0  
},  
"ts" : null  
}  
}

Response:

{  
"key\_as\_string" : "1553299200000",  
"key" : 1553299200000,  
"doc\_count" : 2,  
"genres" : {  
"doc\_count\_error\_upper\_bound" : 0,  
"sum\_other\_doc\_count" : 0,  
"buckets" : [  
{  
"key" : "zzz",  
"doc\_count" : 1,  
"the\_avg" : {  
"value" : 11864.0  
}  
},  
{  
"key" : "aaaaa",  
"doc\_count" : 1,  
"the\_avg" : {  
"value" : 11864.0  
}  
}  
]  
}  
}

Query until nested term aggregation with avg:  
POST storagemetric/metric/\_search  
{  
"size": 0,  
"aggs": {  
"time-filter": {  
"filter": {  
"range": {  
"createdTimestamp": {  
"lte": "now",  
"gt": "now-5M"  
}  
}  
},  
"aggs": {  
"my\_date\_histo": {  
"date\_histogram": {  
"field": "createdTimestamp",  
"interval": "day"  
},  
"aggs": {  
"genres": {  
"terms": {  
"field": "name.keyword"  
},  
"aggs": {  
"the\_avg": {  
"avg": {  
"field": "attributes.all\_bssize\_average\_bytes"

```
              }
            }
          }
        }
      }
    }
  }
}

```

}  
}

---

<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:** [June 19, 2019, 4:06pm UTC](https://discuss.elastic.co/t/cumulative-sum-query-inside-nested-term-aggregation-avg-aggregation/182276/2 "2019-06-19T16:06:56Z")

</div>

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