# ES SQL - Order by Count

**URL:** <https://discuss.elastic.co/t/es-sql-order-by-count/248195>\
**Category:** Elasticsearch\
**Created:** [September 10, 2020, 3:13pm UTC](https://discuss.elastic.co/t/es-sql-order-by-count/248195 "2020-09-10T15:13:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jose\_H](https://avatars.discourse-cdn.com/v4/letter/j/e99b99/32.png) [@Jose\_H](https://discuss.elastic.co/u/Jose_H)\
**Post date:** [September 10, 2020, 3:13pm UTC](https://discuss.elastic.co/t/es-sql-order-by-count/248195/1 "2020-09-10T15:13:53Z")

</div>

Hi all,

Im trying to order and display by the highest count for @computed.message field, but encountering the below error:

```
POST /_sql?format=txt
 {
   "query":"SELECT \"@timestamp\", \"@computed.message\", \"@computed.blocked\", \"sourceIpAddress\" FROM \"csco-fmc-ms\" where \"@timestamp\" > now() - interval 60 minutes AND \"@computed.message\" IS NOT NULL ORDER BY COUNT(*)"
 } 

```

> {  
> "error" : {  
> "root\_cause" : [  
> {  
> "type" : "sql\_illegal\_argument\_exception",  
> "reason" : "Cannot find backing column for ordering aggregation [org.elasticsearch.xpack.sql.querydsl.container.AggregateSort@da22d608]"  
> }  
> ],  
> "type" : "sql\_illegal\_argument\_exception",  
> "reason" : "Cannot find backing column for ordering aggregation [org.elasticsearch.xpack.sql.querydsl.container.AggregateSort@da22d608]"  
> },  
> "status" : 500  
> }

Any help will be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![bogdan.pintea](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bogdan.pintea/32/45740_2.png) [@bogdan.pintea](https://discuss.elastic.co/u/bogdan.pintea)\
**Post date:** [September 11, 2020, 6:58am UTC](https://discuss.elastic.co/t/es-sql-order-by-count/248195/2 "2020-09-11T06:58:28Z")

</div>

The error is a bit cryptic and could probably be improved, but what it tries to say is that there's nothing to count on as requested in the `ORDER BY COUNT(*)`: you need a grouping (i.e. an aggregation) to be counted, if you want to order by its cardinality. So it's an SQL semantical error.

A potential example: `SELECT "@computed.message" FROM ... [WHERE ...] GROUP BY "@computed.message" ORDER BY COUNT(*)`.

---

<div class="post-metadata">

**Author:** ![bogdan.pintea](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bogdan.pintea/32/45740_2.png) [@bogdan.pintea](https://discuss.elastic.co/u/bogdan.pintea)\
**Post date:** [September 11, 2020, 7:02am UTC](https://discuss.elastic.co/t/es-sql-order-by-count/248195/3 "2020-09-11T07:02:51Z")

</div>

> [@Jose\_H](#):
>
> ```auto
> POST /_sql?format=txt
> {
> "query":"SELECT \"@timestamp\", \"@computed.message\", \"@computed.blocked\", \"sourceIpAddress\" FROM \"csco-fmc-ms\" where \"@timestamp\" > now() - interval 60 minutes AND \"@computed.message\" IS NOT NULL ORDER BY COUNT(*)"
> } 
> 
> ```

Just a tip: if you're using Kibana Console, you can use the triple quotes for the query value, to avoid quotes escaping within the SQL query:

```auto
POST /_sql?format=txt
 {
   "query": """SELECT "@timestamp", ... """
 } 

```

---

<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:** [October 9, 2020, 7:02am UTC](https://discuss.elastic.co/t/es-sql-order-by-count/248195/4 "2020-10-09T07:02:59Z")

</div>

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