Assuming the table name = table name and the column name is = sex, we can do so:
select
sexo,
count(*) as totalSexo,
count(*) / (select count(*) from tabelax) as Percentagem
from tabelax
group by sexo
Example table:
Upshot:
If you want the results in percentage form rounded to 2 decimal places this row
count(*) / (select count(*) from tabelax) as Percentagem
for
round((count(*) / (select count(*) from tabelax)*100),2) as `%`
Or if you’d rather not %
as an alias for the column, by
round((count(*) / (select count(*) from tabelax)*100),2) as Percentagem
Upshot:
Round - Returns a rounded numerical value for the specified length or accuracy.
This function takes two parameters: the first of them is the number to be formatted, and the second is the number of decimals that the user wants the value to display. In the default of the second parameter, the command rounds the value to an integer (no decimal places).
thus, in the same column separated by a point and a comma?
– user60252
Hello, no, this was an example for easier understanding, the data is inside a mysql database. With this example I get all table data (SELECT COUNT(id) AS total from users) looks like this: 1.400.005. In this case I get all users. Now I want to take a table called Sex, I need to take the amount of each of the items (Trans, Female and Male) and do the percentage, if possible in a single command.
– Claudio Soares