# Is SUBQUERY possible?

**URL:** <https://discuss.elastic.co/t/is-subquery-possible/275043>\
**Category:** Kibana\
**Tags:** elastic-stack-sql\
**Created:** [June 5, 2021, 10:05pm UTC](https://discuss.elastic.co/t/is-subquery-possible/275043 "2021-06-05T22:05:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jakub\_Kaczmarek](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jakub_kaczmarek/32/77810_2.png) [@Jakub\_Kaczmarek](https://discuss.elastic.co/u/Jakub_Kaczmarek)\
**Post date:** [June 5, 2021, 10:05pm UTC](https://discuss.elastic.co/t/is-subquery-possible/275043/1 "2021-06-05T22:05:53Z")

</div>

Hi,

I have an index "inputs", which consists of 2 columns: **sessionId** (string), **timestamp** (date).  
I want to calculate **average session duration** and try to add visualization to Canvas.

SQL formula:  
SELECT AVG(duration)  
FROM  
(SELECT MAX(timestamp)-MIN(timestamp) AS duration  
FROM inputs  
GROUP BY sessionId)

As I see, it would be also needed first to cast timestamp to numeric (BIGINT).  
_[MAX(timestamp) - MIN(timestamp)] must be [numeric], found value [MAX(timestamp)] type [datetime]_

So I changed SQL query to:  
SELECT  
MAX(time) - MIN(time) as duration  
FROM  
(SELECT sessionId, CAST(timestamp as BIGINT) AS time  
FROM "inputs")  
GROUP BY sessionId

Now I'm getting error:  
_[math] \> Failed to execute math expression. Check your column names_

As I assume it's not supported within Elasticsearch:  
_Using sub-selects ( `SELECT X FROM (SELECT Y)` ) is **supported to a small degree** : any sub-select that can be "flattened" into a single `SELECT` is possible with Elasticsearch SQL._  
[Elasticsearch SQL limitations](https://www.elastic.co/guide/en/elasticsearch/reference/current/sql-limitations.html)

Could I get confirmation that it's not possible within Elasticsearch to manipulate index in such a way to visualize **average session duration** using Kibana Canvas or Kibana Dashboard panel (I'm expected to do it through Kibana Dashboard panel)?

More general question:  
As I understand Elasticsearch is not a tool that supports complex queries like **average session duration**?  
Is the nature of Elasticsearch to do calculations based on a single row of index, not based on the whole index (GROUP BY) and that's incorrect use of Elasticsearch ( **average session duration** ) query?

I'd be extremely thankful for help,  
Kuba

---

<div class="post-metadata">

**Author:** ![matriv](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matriv/32/43656_2.png) [@matriv](https://discuss.elastic.co/u/matriv)\
**Post date:** [June 7, 2021, 10:06am UTC](https://discuss.elastic.co/t/is-subquery-possible/275043/2 "2021-06-07T10:06:25Z")

</div>

What you're trying to do is to have a 2nd level aggregation (AVG()) on top of the MIN() MAX() aggregations which unfortunately is not currently supported by ES SQL. I'm not sure what can be done in Kibana/Canvas on top of the basic MIN() MAX() query in order to get the AVG of this subtraction that you're looking for, so let's see if someone from Kibana has a workaround.

---

<div class="post-metadata">

**Author:** ![Jakub\_Kaczmarek](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jakub_kaczmarek/32/77810_2.png) [@Jakub\_Kaczmarek](https://discuss.elastic.co/u/Jakub_Kaczmarek)\
**Post date:** [June 7, 2021, 11:30am UTC](https://discuss.elastic.co/t/is-subquery-possible/275043/3 "2021-06-07T11:30:38Z")

</div>

thx for response 🙂

---

<div class="post-metadata">

**Author:** ![Jakub\_Kaczmarek](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jakub_kaczmarek/32/77810_2.png) [@Jakub\_Kaczmarek](https://discuss.elastic.co/u/Jakub_Kaczmarek)\
**Post date:** [June 10, 2021, 11:50am UTC](https://discuss.elastic.co/t/is-subquery-possible/275043/4 "2021-06-10T11:50:05Z")

</div>

Hi Marios,

could you ping someone from Kibana?,

Thanks,  
Kuba

---

<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 8, 2021, 11:50am UTC](https://discuss.elastic.co/t/is-subquery-possible/275043/5 "2021-07-08T11:50:32Z")

</div>

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