# Find maximum and minimum time difference

**URL:** https://discuss.elastic.co/t/find-maximum-and-minimum-time-difference/254126
**Category:** Elasticsearch
**Created:** [November 3, 2020, 9:51am UTC](https://discuss.elastic.co/t/find-maximum-and-minimum-time-difference/254126 "2020-11-03T09:51:58Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![rohitarorait82](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rohitarorait82/32/82981_2.png) [@rohitarorait82](https://discuss.elastic.co/u/rohitarorait82)
#### Post date: [November 3, 2020, 9:51am UTC](https://discuss.elastic.co/t/find-maximum-and-minimum-time-difference/254126/1 "2020-11-03T09:51:59Z")

</div>

Hi All,

Is there a way to find out the maximum and minimum time difference for 3rd column in the query below; whenever I am trying to order it by 3rd column I am getting below error

**Cannot order by non-grouped column**

Query :

```
POST /_sql?format=txt
{
  "query": "SELECT SERVICE_NAME,CHANNEL_NAME, CASE WHEN TIME_3 <> 'T' and TIME_1 <> 'T' THEN DATEDIFF('seconds', CAST(TIME_1 AS DATETIME), CAST(TIME_3 AS DATETIME)) ELSE 0 END AS DiffInSeconds FROM \"Tem_visualization*\" WHERE \"@timestamp\" >= NOW() - INTERVAL 10 Minutes GROUP BY SERVICE_NAME, CHANNEL_NAME, TIME_3, TIME_1 order by 3 desc"
}
```

---

<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: [December 1, 2020, 9:52am UTC](https://discuss.elastic.co/t/find-maximum-and-minimum-time-difference/254126/2 "2020-12-01T09:52:01Z")

</div>

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