# Difference between consecutive records using derivative aggregation

**URL:** <https://discuss.elastic.co/t/difference-between-consecutive-records-using-derivative-aggregation/123901>\
**Category:** Elasticsearch\
**Created:** [March 14, 2018, 12:24pm UTC](https://discuss.elastic.co/t/difference-between-consecutive-records-using-derivative-aggregation/123901 "2018-03-14T12:24:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vinayakfutak](https://avatars.discourse-cdn.com/v4/letter/v/bb73d2/32.png) [@vinayakfutak](https://discuss.elastic.co/u/vinayakfutak)\
**Post date:** [March 14, 2018, 12:24pm UTC](https://discuss.elastic.co/t/difference-between-consecutive-records-using-derivative-aggregation/123901/1 "2018-03-14T12:24:24Z")

</div>

These are the records inserted in my index  
{  
{  
"\_index": "device\_beacon\_log\_test",  
"\_type": "device\_beacon\_log\_test",  
"\_id": "AWIJlB6WpDisa0DNP7rG",  
"\_score": 1,  
"\_source": {  
"place\_id": 61,  
"device\_time": "2018-02-12T14:22:59Z"

}  
},  
{  
"\_index": "device\_beacon\_log\_test",  
"\_type": "device\_beacon\_log\_test",  
"\_id": "AWIJlB6WpDisa0DNP7rG",  
"\_score": 1,  
"\_source": {  
"place\_id": 61,  
"device\_time": "2018-02-12T14:23:59Z"

}  
},  
{  
"\_index": "device\_beacon\_log\_test",  
"\_type": "device\_beacon\_log\_test",  
"\_id": "AWIJlB6WpDisa0DNP7rG",  
"\_score": 1,  
"\_source": {  
"place\_id": 61,  
"device\_time": "2018-02-12T14:45:59Z"

}  
}  
}

I want to calculate the difference between device time for each consecutive records

here is my query  
{  
"size": 0,  
"aggs": {  
"sales\_per\_month": {  
"date\_histogram": {  
"field": "device\_time",  
"format": "dd-MM-yyyy",  
"interval": "day"  
},  
"aggs": {  
"a1": {  
"terms": {  
"field": "device\_time",  
"size": 500,  
"order": {  
"the\_sum": "asc"  
}  
},  
"aggs": {  
"the\_sum": {  
"sum": {  
"field": "device\_time"  
}  
},  
"derivative": {  
"derivative": {  
"buckets\_path": "the\_sum"  
}  
}  
}  
}  
}  
}  
}  
}

I want to calculate the difference between the\_sum bucket .but query is failed to produce the result??can anyone help me to solve this

this is the output that I needed

{  
"key\_as\_string": "12-02-2018",  
"key": 1518393600000,  
"doc\_count": 14,  
"a1": {  
"doc\_count\_error\_upper\_bound": 0,  
"sum\_other\_doc\_count": 0,  
"buckets": [  
{  
"key": 1518444239000,  
"key\_as\_string": "2018-02-12T14:03:59.000Z",  
"doc\_count": 1,  
"device\_time": {  
"value": 1518444239000  
}  
},  
{  
"key": 1518444359000,  
"key\_as\_string": "2018-02-12T14:05:59.000Z",  
"doc\_count": 1,  
"device\_time": {  
"value": 1518444359000  
},  
"derivative": {  
"value": 120000  
}  
},  
{  
"key": 1518445319000,  
"key\_as\_string": "2018-02-12T14:21:59.000Z",  
"doc\_count": 1,  
"device\_time": {  
"value": 1518445319000  
},  
"derivative": {  
"value": 960000  
}  
},  
{  
"key": 1518445379000,  
"key\_as\_string": "2018-02-12T14:22:59.000Z",  
"doc\_count": 1,  
"device\_time": {  
"value": 1518445379000  
},  
"derivative": {  
"value": 60000  
}  
}  
]  
}  
}

but when I am trying to execute query it's giving error as a ,derivative aggregation [derivative] must have a histogram or date\_histogram as parent.So can I perform derivative aggregation without date\_histogram??or there is any other way to perform this operation

---

<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:** [April 11, 2018, 12:24pm UTC](https://discuss.elastic.co/t/difference-between-consecutive-records-using-derivative-aggregation/123901/2 "2018-04-11T12:24:38Z")

</div>

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