# Nested Aggregation On Date Differance

**URL:** <https://discuss.elastic.co/t/nested-aggregation-on-date-differance/137403>\
**Category:** Elasticsearch\
**Created:** [June 26, 2018, 9:19am UTC](https://discuss.elastic.co/t/nested-aggregation-on-date-differance/137403 "2018-06-26T09:19:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohamed-assem92](https://avatars.discourse-cdn.com/v4/letter/m/ec9cab/32.png) [@mohamed-assem92](https://discuss.elastic.co/u/mohamed-assem92)\
**Post date:** [June 26, 2018, 9:19am UTC](https://discuss.elastic.co/t/nested-aggregation-on-date-differance/137403/1 "2018-06-26T09:19:14Z")

</div>

Hello,  
i have data each document in it have history array like following

......  
"history" : [  
{  
"user" : {  
"id" : "14",  
"firstName" : "user14",  
"lastName" : "user14",  
"username" : "user14",  
"principalType" : "user"  
},  
"time" : "2018-06-25T01:05:20Z",  
"action" : "create",  
"newStatus" : "in progress",  
"isEdit" : false,  
"data" : { }  
},  
{  
"user" : {  
"id" : "5b2d98a1c4fae4a5ab10e841",  
"firstName" : "Ramy",  
"lastName" : "Ebrahim",  
"username" : "Ramy",  
"principalType" : "ministry-employee"  
},  
"time" : "2018-06-30T18:37:21Z",  
"action" : "patchAttributes",  
"status" : "start",  
"newStatus" : "review",  
"isEdit" : false,  
"data" : { }  
},  
{  
"user" : {  
"id" : "5b2d98a1c4fdfda45cb10e841",  
"firstName" : "Yasser",  
"lastName" : "Mohamed",  
"username" : "Yasser",  
"principalType" : "ministry-employee"  
},  
"time" : "2018-07-01T23:28:27Z",  
"action" : "patchAttributes",  
"status" : "review",  
"newStatus" : "confirm1",  
"isEdit" : false,  
"data" : { }  
}  
]

i want to aggregate on the newStatus to get the average response time of each status depending on time diff , each object have newStatus and time attributes what i want to do for each status subtract the time of the previous status from current status time then get the average response of of each status within bucket like this

result  
"buckets": [  
{  
"key": "in progress",  
"doc\_count": 100,  
"responseTime": {  
"value": 1.48  
}  
},  
{  
"key": "review",  
"doc\_count": 88,  
"responseTime": {  
"value": 1.5454545454545454  
}  
},  
{  
"key": "confirm1",  
"doc\_count": 47,  
"responseTime": {  
"value": 2.021276595744681  
}  
},  
{  
"key": "rejected",  
"doc\_count": 28,  
"responseTime": {  
"value": 1.4285714285714286  
}  
},  
{  
"key": "confirm2",  
"doc\_count": 23,  
"responseTime": {  
"value": 1  
}  
},  
{  
"key": "completed",  
"doc\_count": 15,  
"responseTime": {  
"value": 1  
}  
}  
]

---

<div class="post-metadata">

**Author:** ![mohamed-assem92](https://avatars.discourse-cdn.com/v4/letter/m/ec9cab/32.png) [@mohamed-assem92](https://discuss.elastic.co/u/mohamed-assem92)\
**Post date:** [June 26, 2018, 11:07am UTC](https://discuss.elastic.co/t/nested-aggregation-on-date-differance/137403/2 "2018-06-26T11:07:33Z")

</div>

any support !!

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 26, 2018, 1:46pm UTC](https://discuss.elastic.co/t/nested-aggregation-on-date-differance/137403/4 "2018-06-26T13:46:16Z")

</div>

Read [this](https://discuss.elastic.co/t/about-the-elasticsearch-category/21) and specifically the "Also be patient" part.

It's fine to answer on your own thread after 2 or 3 days (not including weekends) if you don't have an answer.

---

<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 24, 2018, 1:46pm UTC](https://discuss.elastic.co/t/nested-aggregation-on-date-differance/137403/5 "2018-07-24T13:46:27Z")

</div>

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