# Time difference

**URL:** <https://discuss.elastic.co/t/time-difference/276300>\
**Category:** Elasticsearch\
**Created:** [June 17, 2021, 6:23pm UTC](https://discuss.elastic.co/t/time-difference/276300 "2021-06-17T18:23:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cgr\_EQ](https://avatars.discourse-cdn.com/v4/letter/c/f14d63/32.png) [@Cgr\_EQ](https://discuss.elastic.co/u/Cgr_EQ)\
**Post date:** [June 17, 2021, 6:23pm UTC](https://discuss.elastic.co/t/time-difference/276300/1 "2021-06-17T18:23:11Z")

</div>

I have 2 documents and each document fields are as follow:

## Timestamp / ID / Field

2021-06-15 12:00:00 / 1 / Start (doc1)  
2021-06-15 12:00:05 / 1 / End (doc2)

How do I get a time difference between Start and End with ID=1 in the Dev Tools ?

I can do two SQL transactions to grab both start and end timestamp and compute the difference but this operation is not efficient if I do it for thousands of documents.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [June 21, 2021, 9:05am UTC](https://discuss.elastic.co/t/time-difference/276300/2 "2021-06-21T09:05:56Z")

</div>

You might want to take a look at [transforms](https://www.elastic.co/guide/en/elasticsearch/reference/7.13/transforms.html) in order to create summary indices that contain exactly the data you need in a single document in order to create concise and fast queries.

hope this helps!

---

<div class="post-metadata">

**Author:** ![Cgr\_EQ](https://avatars.discourse-cdn.com/v4/letter/c/f14d63/32.png) [@Cgr\_EQ](https://discuss.elastic.co/u/Cgr_EQ)\
**Post date:** [June 23, 2021, 4:10am UTC](https://discuss.elastic.co/t/time-difference/276300/3 "2021-06-23T04:10:07Z")

</div>

Thank you for your reply.

I created a transform and selected a grouping by ID.  
In the aggregations fields, I selected Timestamp max and Timestamp min .

## Timestamp / ID / Field

2021-06-15 12:00:00 / 1 / Start  
2021-06-15 12:00:05 / 1 / Running  
2021-06-16 1:00:01 / 1 / End

In this particular example, Timestamp.max would give me a value of 1:00:01 (Field is End) and Timestamp.min would give me 12:00:00 (Field is Start).

How would I change my aggregation if i wanted Timestamp.max to be the value where Field is 'Running', ie 12:00:05 ?

Also, is there a way to add another colum called duration and compute the timestamp max - timestamp min during the transform ?

---

<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 21, 2021, 4:11am UTC](https://discuss.elastic.co/t/time-difference/276300/4 "2021-07-21T04:11:00Z")

</div>

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