I have the following query:
select count(*), floor(weight / 10) * 10
from patients
group by (floor(weight / 10) * 10)
order by floor(weight / 10) * 10 desc
But I want to convert it to use alias. But I know that in SQL order of operation GROUP BY
is executed before SELECT
which means I need to define an alias in the GROUP BY
statement. But when I do I get an error. What's the SQL compliant way of doing this?
This SQL goes wrong and complains of the AS
usage in the GROUP BY
statement:
select count(*), weight_group
from patients
group by (floor(weight / 10) * 10) AS weight_group
order by weight_group desc