Assuming that the table name = tablex and the column name is = sex, we can do this:
select
sexo,
count(*) as totalSexo,
count(*) / (select count(*) from tabelax) as Percentagem
from tabelax
group by sexo
Sample table:
Result:
Ifyouwanttheresultsasapercentageroundedto2decimalplacesreplacethisline
count(*)/(selectcount(*)fromtabelax)asPercentagem
by
round((count(*)/(selectcount(*)fromtabelax)*100),2)as'%'
Orifyouprefernottouse%
asacolumnalias,by
round((count(*)/(selectcount(*)fromtabelax)*100),2)asPercentagem
Result:
Round - Returns a numeric value, rounded, to the specified length or precision.
This function receives two parameters: the first one is the number to be formatted, and the second is the number of decimal places the user wants the value to display. In the default of the second parameter, the command rounds the value to an integer (without decimals).