Mysql
 sql >> Database >  >> RDS >> Mysql

Come posso ottenere la percentuale di righe totali con mysql per un gruppo?

Sì, puoi:

select num, string, 100 * num / total as percent
from (
    select count(*) as num, string
    from useragent_ip
    left join useragents on useragent_id = useragents.id
    group by useragent_id) x
cross join (
    select count(*) as total
    from useragent_ip
    left join useragents on useragent_id = useragents.id) y
order by num desc, string;

Ho rimosso il having num > 2 , perché non sembrava avere senso.