Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL: ORDER BY COUNT() throws error #32870

Closed
EthanStrider opened this issue Aug 15, 2018 · 8 comments
Closed

SQL: ORDER BY COUNT() throws error #32870

EthanStrider opened this issue Aug 15, 2018 · 8 comments
Labels

Comments

@EthanStrider
Copy link

Elasticsearch version (v6.3.1):

Description of the problem including expected versus actual behavior:

I’m trying to list the counts of each city in descending order.
For example, if Paris occurred 100 times in the database, London occurred 25 times, and Sydney 3, then I would like the output to be listed with Paris first, then London, then Sydney.

name      count
Paris     100
London    25
Sydney    3

However, the following SQL query throws an error:

POST /_xpack/sql?format=txt
{
    "query": "SELECT geoip.city_name AS name, COUNT(geoip.city_name) AS count FROM \"cyclops-sessions\" GROUP BY geoip.city_name ORDER BY count DESC"
}

Error

{
  "error": {
    "root_cause": [
      {
        "type": "verification_exception",
        "reason": "Found 1 problem(s)\nline 1:123: Cannot order by non-grouped column [COUNT(1)], expected [geoip.city_name]"
      }
...
  "status": 400
}
@EthanStrider EthanStrider added the :Analytics/SQL SQL querying label Aug 15, 2018
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-search-aggs

@astefan
Copy link
Contributor

astefan commented Aug 16, 2018

While this is a valid and probably frequently used type of functionality, in ES-SQL this is not possible (at the moment) due to how the composite aggregation (used behind the scene to perform the grouping and ordering) works: it can only order by the key (the value). I do have a concern, though, regarding the error message. More about this in this issue.

@madminions
Copy link

ES v - 6.4.1
If you do like this, no error is thrown
POST /_xpack/sql?format=txt
{
"query": "SELECT name,count from (SELECT geoip.city_name AS name, COUNT(geoip.city_name) AS count FROM "cyclops-sessions" GROUP BY geoip.city_name) ORDER BY count DESC"
}

But still it doesn't return the query sorted in DESC order.
Any workaround for this query ?

If possible i would like to work on this new feature.

@paulcarey
Copy link

Having the ability to order by a count would be very useful indeed. The following comment implies that this might be possible at some point in future (emphasis mine).

While this is a valid and probably frequently used type of functionality, in ES-SQL this is not possible (at the moment)

But this issue has a won't fix label.

Does this mean there are no near-term plans to support this feature, but that it's not entirely ruled out?

@costin
Copy link
Member

costin commented Jan 17, 2019

There's a separate issue (#35118) that tracks this feature which is currently being investigated. It's best to monitor that for progress.

@costin
Copy link
Member

costin commented Feb 2, 2019

As FYI, this feature is now supported and merged into 7.x and upcoming 6.7.

@kkmathigir
Copy link

kkmathigir commented Feb 3, 2019

I am in need of this feature big time. Is there a patch that can be made available for 6.6? This is quite helpful for our Canvas implementation. Or any workaround in 6.6?

@abubogale342
Copy link

I have almost similar issue. count is working fine for me. But I got one problem. My query is like below:

select * from (select street, count(street) as counts, min(grocery) as grocery, min(school) as school, (min(grocery) + min(school)) as score from geo_address group by 1) t order by grocery

I have geo_address index and I am creating column on the fly which is score that is sum of grocery column and school column.
Ther result :
image
Now it is sorted based on the grocery but I want to sort based on score. when I try to sort by it I get an error : - Cannot order by non-grouped column [score]. can anyone please give me any workaround to solve this problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants