# Computing number of days between 2 dates in a query

**URL:** <https://discuss.elastic.co/t/computing-number-of-days-between-2-dates-in-a-query/16958>\
**Category:** Elasticsearch\
**Created:** [April 11, 2014, 1:52pm UTC](https://discuss.elastic.co/t/computing-number-of-days-between-2-dates-in-a-query/16958 "2014-04-11T13:52:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vincent\_Massol](https://avatars.discourse-cdn.com/v4/letter/v/ac91a4/32.png) [@Vincent\_Massol](https://discuss.elastic.co/u/Vincent_Massol)\
**Post date:** [April 11, 2014, 1:52pm UTC](https://discuss.elastic.co/t/computing-number-of-days-between-2-dates-in-a-query/16958/1 "2014-04-11T13:52:54Z")

</div>

Hi guys,

I'm using ES to receive pings from clients with the following data sent  
(see [http://design.xwiki.org/xwiki/bin/view/Proposal/ActiveInstalls2](http://design.xwiki.org/xwiki/bin/view/Proposal/ActiveInstalls2) for  
more context if you need the full picture):

curl -XPOST "[http://localhost:9200/installs/install?timestamp=2014-02-20](http://localhost:9200/installs/install?timestamp=2014-02-20)"  
-d'  
{  
"formatVersion" : "2.0",  
"instanceId" : "abc",  
"distributionId" : "org.xwiki.enterprise:xwiki-enterprise-web",  
"distributionVersion" : "6.0-milestone-1"  
}'

I'd like to compute the average elapsed days clients take to upgrade their  
versions (ie when distributionVersion changes). So far I've done this:

{  
"aggs": {  
"instanceId\_count" : {  
"terms" : { "field" : "instanceId" },  
"aggs" : {  
"versions" : {  
"terms" : { "field" : "distributionVersion" },  
"aggs" : {  
"date\_stats" : {  
"stats" : { "field" : "\_timestamp" }  
}  
}  
}  
}  
}  
}  
}

Now this returns data such as:

"aggregations": {  
"instanceId\_count": {  
"buckets": [  
{  
"key": "abc",  
"doc\_count": 2,  
"versions": {  
"buckets": [  
{  
"key": "6.0-milestone-1",  
"doc\_count": 1,  
"date\_stats": {  
"count": 1,  
"min": 1392854400000,  
"max": 1392854400000,  
"avg": 1392854400000,  
"sum": 1392854400000  
}  
},  
...

The next step is to compute the difference between date\_stats.max and  
date\_stats.min to get the number of days. And then I'd need to do a global  
average on the resulting days. Is it possible? 🙂

Is it possible to use scripting to reference the result of an upstream  
aggregation?

Thanks a lot for any pointer!  
-Vincent

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/e29004f0-8595-4e9f-a2d2-876182cc9ca0%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/e29004f0-8595-4e9f-a2d2-876182cc9ca0%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 6, 2017, 1:36am UTC](https://discuss.elastic.co/t/computing-number-of-days-between-2-dates-in-a-query/16958/2 "2017-07-06T01:36:30Z")

</div>


