# Elasticsearch 7.0 SQL ACCESS, Priority of multiple order by commands

**URL:** <https://discuss.elastic.co/t/elasticsearch-7-0-sql-access-priority-of-multiple-order-by-commands/185578>\
**Category:** Elasticsearch\
**Created:** [June 13, 2019, 8:30am UTC](https://discuss.elastic.co/t/elasticsearch-7-0-sql-access-priority-of-multiple-order-by-commands/185578 "2019-06-13T08:30:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![c81b4c93b1f70af5398f](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/c81b4c93b1f70af5398f/32/45070_2.png) [@c81b4c93b1f70af5398f](https://discuss.elastic.co/u/c81b4c93b1f70af5398f)\
**Post date:** [June 13, 2019, 8:30am UTC](https://discuss.elastic.co/t/elasticsearch-7-0-sql-access-priority-of-multiple-order-by-commands/185578/1 "2019-06-13T08:30:17Z")

</div>

This is the query I'm trying below.  
`SELECT "hour", "gender", SUM("aggrCount") AS "cnt" FROM "my.index" WHERE "batchDate" < XXX AND \"batchDate\" >= XXX AND "hour" in ( 10, 11, 12, 13, 14) GROUP BY "hour", "gender" **ORDER BY "hour" ASC, "cnt" DESC** `

Here is described as REST API.  
POST /\_sql?format=json  
{  
"query": "SELECT "hour", "gender", SUM("aggrCount") AS "cnt" FROM "my.index" WHERE "batchDate" \< XXX AND "batchDate" \>= XXX AND "hour" in ( 10, 11, 12, 13, 14) GROUP BY "hour", "gender" ORDER BY "hour" ASC, "cnt" DESC"  
}

I indicated order by commands using two values of "hour" and "cnt", but the result was ordered by "cnt" first :  
`{"columns":[{"name":"hour","type":"long"},{"name":"gender","type":"text"},{"name":"cnt","type":"long"}],"rows":[[10,"f",46.0],[10,"m",43.0],[11,"m",16.0],[12,"m",6.0],[11,"f",2.0],[12,"f",2.0],[14,"m",2.0],[13,"m",1.0]]}`

And It works in the same way even if the order by commands are swapped into the reverse order (ORDER BY "cnt" DESC, "hour" ASC).  
**Is there any way to specify the priority of multiple order by commands?**

---

<div class="post-metadata">

**Author:** ![Andrei\_Stefan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrei_stefan/32/47533_2.png) [@Andrei\_Stefan](https://discuss.elastic.co/u/Andrei_Stefan)\
**Post date:** [June 14, 2019, 6:42am UTC](https://discuss.elastic.co/t/elasticsearch-7-0-sql-access-priority-of-multiple-order-by-commands/185578/2 "2019-06-14T06:42:05Z")

</div>

Hi @c81b4c93b1f70af5398f,

I think you stumbled upon this bug: [https://github.com/elastic/elasticsearch/issues/42851](https://github.com/elastic/elasticsearch/issues/42851).  
Lukily, it was fixed yesterday and the fix should be available in 7.1.2 as the earliest version. See [this PR](https://github.com/elastic/elasticsearch/pull/43154) for the complete list of versions where the fix will be in.

---

<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 12, 2019, 6:42am UTC](https://discuss.elastic.co/t/elasticsearch-7-0-sql-access-priority-of-multiple-order-by-commands/185578/3 "2019-07-12T06:42:05Z")

</div>

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