Skip to content
Advertisement

Group or Aggregate functions (min(),max(),sum(),count(),…etc.,) cannot be used within the Group by/Order by/Where/ON clause

It seems that I can not order by the count of a column which is odd because I have seen plenty of examples where the count() is used in the ORDER BY statement. I am using Zoho Analytics. Here is my query:

SELECT "Lead Owner Name", "Lead Source", count("Lead Source")
FROM "Leads"
group by "Lead Owner Name", "Lead Source"
order by count("Lead Source") DESC;

Advertisement

Answer

Try using an alias:

SELECT "Lead Owner Name", "Lead Source", count("Lead Source") as cnt
FROM "Leads" 
GROUP BY "Lead Owner Name", "Lead Source" 
ORDER BY cnt DESC;

Most databases support arbitrary expressions in the ORDER BY. However, some require that the sorting expression actually be a column — and they don’t detect when a SELECT expression is the same as the ORDER BY expression. I’ve hit this problem before (I think with Hive) and just thought this might work in your environment.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement