# How to get Sum of a particular field for current month and sum of that field for the last month till date?

**URL:** <https://discuss.elastic.co/t/how-to-get-sum-of-a-particular-field-for-current-month-and-sum-of-that-field-for-the-last-month-till-date/29475>\
**Category:** Elasticsearch\
**Created:** [September 17, 2015, 11:38am UTC](https://discuss.elastic.co/t/how-to-get-sum-of-a-particular-field-for-current-month-and-sum-of-that-field-for-the-last-month-till-date/29475 "2015-09-17T11:38:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![anusha6](https://avatars.discourse-cdn.com/v4/letter/a/a5b964/32.png) [@anusha6](https://discuss.elastic.co/u/anusha6)\
**Post date:** [September 17, 2015, 11:38am UTC](https://discuss.elastic.co/t/how-to-get-sum-of-a-particular-field-for-current-month-and-sum-of-that-field-for-the-last-month-till-date/29475/1 "2015-09-17T11:38:23Z")

</div>

Hi All,

"\_index": "xmlactivity\_prod\_201501", This index is for 2015-jan month for feb :" xmlactivity\_prod\_201502" and so on.  
"\_type": "xmlactivityv2",

I have my sample data as shown below:

```
      "created": "2015-01-15T17:08:45.000Z",
      "netamt": 0

```

Have two fields and their mapping as shown :  
"created": {  
"type": "date",  
"format": "dateOptionalTime"  
}  
"netamt": {  
"type": "double"  
}

**I would like to calculate netamt sum for current month current year and for the last month current year till date**

For this am using a query:

GET xmlactivity\_prod\_2015\*/\_search  
{  
"size": 0,  
"aggs": {  
"articles\_over\_time": {  
"date\_histogram": {  
"field": "created",  
"format": "yyyy-MM-dd",  
"interval": "month",  
"min\_doc\_count": 1  
},  
"aggs": {  
"netamt\_sum": {  
"sum": {  
"field": "netamt"  
}  
}  
}  
}  
}  
}

This is giving the output as complete month netamt sum for every month.

Response:  
"buckets": [  
{  
"key\_as\_string": "2015-07-01",  
"key": 1435708800000,  
"doc\_count": 7614791,  
"netamt\_sum": {  
"value": 23869820.07780247  
}  
},  
{  
"key\_as\_string": "2015-08-01",  
"key": 1438387200000,  
"doc\_count": 7311611,  
"netamt\_sum": {  
"value": 22785676.593002092  
}  
},  
{  
"key\_as\_string": "2015-09-01",  
"key": 1441065600000,  
"doc\_count": 3896206,  
"netamt\_sum": {  
"value": 12117393.990000023  
}  
}  
]

**But I would like to get current month netamt sum and last month netamt sum till date, similarly for current year netamt sum and last year netamt sum till date ??????????**

Can any one help me out for to resolve this scenario?????

---

<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 5, 2017, 11:49pm UTC](https://discuss.elastic.co/t/how-to-get-sum-of-a-particular-field-for-current-month-and-sum-of-that-field-for-the-last-month-till-date/29475/2 "2017-07-05T23:49:46Z")

</div>


