# Kibana SQL question - subselect

**URL:** https://discuss.elastic.co/t/kibana-sql-question-subselect/265229
**Category:** Kibana
**Tags:** elastic-stack-sql
**Created:** [February 23, 2021, 4:30pm UTC](https://discuss.elastic.co/t/kibana-sql-question-subselect/265229 "2021-02-23T16:30:32Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![tangkalo](https://avatars.discourse-cdn.com/v4/letter/t/cdc98d/32.png) [@tangkalo](https://discuss.elastic.co/u/tangkalo)
#### Post date: [February 23, 2021, 4:30pm UTC](https://discuss.elastic.co/t/kibana-sql-question-subselect/265229/1 "2021-02-23T16:30:32Z")

</div>

Hi, I am trying to get the latest messages and then sort these messages based on one of their fields(i.e. name in this case). I tried the following. It ran but the sorting didn't happen. Any idea of what I am doing wrong? Thanks in advance for any help.

select tmp.name, tmp.overallStatus from (  
SELECT name, overallStatus FROM "sites"  
ORDER BY "@timestamp" DESC  
LIMIT 80) tmp  
ORDER BY tmp.name asc

---

<div class="post-metadata">

### Author: ![jportner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jportner/32/75692_2.png) [@jportner](https://discuss.elastic.co/u/jportner)
#### Post date: [February 23, 2021, 8:29pm UTC](https://discuss.elastic.co/t/kibana-sql-question-subselect/265229/2 "2021-02-23T20:29:14Z")

</div>

Which part of the sorting didn't happen? Timestamp, name, or both?

I attempted to reproduce this using Kibana's sample data and I did not have any issues with subselect sorting like this.

---

<div class="post-metadata">

### Author: ![tangkalo](https://avatars.discourse-cdn.com/v4/letter/t/cdc98d/32.png) [@tangkalo](https://discuss.elastic.co/u/tangkalo)
#### Post date: [February 23, 2021, 8:30pm UTC](https://discuss.elastic.co/t/kibana-sql-question-subselect/265229/3 "2021-02-23T20:30:35Z")

</div>

> [@tangkalo](#):
>
> ORDER BY name asc

The above order by didn't happen.

---

<div class="post-metadata">

### Author: ![jportner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jportner/32/75692_2.png) [@jportner](https://discuss.elastic.co/u/jportner)
#### Post date: [February 23, 2021, 8:37pm UTC](https://discuss.elastic.co/t/kibana-sql-question-subselect/265229/4 "2021-02-23T20:37:23Z")

</div>

Interesting. Can you try flattening your query? I know this isn't exactly what you have written with the limit, but it's close:

```sql
SELECT name, overallStatus FROM "sites"
ORDER BY "@timestamp" DESC, name ASC
LIMIT 80

```

---

<div class="post-metadata">

### Author: ![tangkalo](https://avatars.discourse-cdn.com/v4/letter/t/cdc98d/32.png) [@tangkalo](https://discuss.elastic.co/u/tangkalo)
#### Post date: [February 23, 2021, 8:49pm UTC](https://discuss.elastic.co/t/kibana-sql-question-subselect/265229/5 "2021-02-23T20:49:34Z")

</div>

> [@jportner](#):
>
> ```auto
> SELECT name, overallStatus FROM "sites"
> ORDER BY "@timestamp" DESC, name ASC
> LIMIT 80
> 
> ```

Yes, they are working without selecting from the subselect but the result is not the same as the one as the original one.

---

<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: [February 24, 2021, 3:23pm UTC](https://discuss.elastic.co/t/kibana-sql-question-subselect/265229/6 "2021-02-24T15:23:14Z")

</div>

Currently only simple subqueries that can be flattened are supported, see: [SQL Limitations | Elasticsearch Reference [7.11] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/sql-limitations.html#_using_a_sub_select)

What you want to achieve is to have a 1st level ordering, limit the results and at a 2nd level re-order those limited results by another sorting column, which is semantically different than flattening the order by (`order by 1, 2` means order by 1 and for equalities on 1 order those entries by 2).

To sum up, what you want to do is not currently supported by ES-SQL.

---

<div class="post-metadata">

### Author: ![tangkalo](https://avatars.discourse-cdn.com/v4/letter/t/cdc98d/32.png) [@tangkalo](https://discuss.elastic.co/u/tangkalo)
#### Post date: [February 24, 2021, 3:40pm UTC](https://discuss.elastic.co/t/kibana-sql-question-subselect/265229/7 "2021-02-24T15:40:06Z")

</div>

@matriv Thanks for confirming.

---

<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: [March 24, 2021, 3:40pm UTC](https://discuss.elastic.co/t/kibana-sql-question-subselect/265229/8 "2021-03-24T15:40:37Z")

</div>

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