Pergunta

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.

Foi útil?

Solução

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
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top