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.

有帮助吗?

解决方案

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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top