Question

I have this sql query:

select GEN_source, count(*) as count, sum(100) / total as percentage
from tics
cross join (select count(*) as total from t_cs) x
group by 1

How to add the order by count ASC using the created alias in the same query ?

Thanks.

Was it helpful?

Solution

Count is a reserved word so it needs to be encased on backtics or change the name

select GEN_source, count(*) as `count`, sum(100) / total as percentage
from tics
cross join (select count(*) as total from t_cs) x
group by 1
ORDER By `count`

or

select GEN_source, count(*) as cnt, sum(100) / total as percentage
from tics
cross join (select count(*) as total from t_cs) x
group by 1
ORDER By cnt
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top