# how to get an average in an sql query?

1

Could you help me solve this doubt I have. I need to show for each blood type that number of employees has it, also showing the total percentage of employees of the company represents that amount.

Here the problem is that in percentage it shows me the number of Id_Type instead of showing a percentage, in this database I only have 3 employees, the percentage that I hope would be 33.33% of each.

asked by Arturo Ortiz Vázquez 23.11.2018 в 01:26
source

2

Welcome to Stackoverflow.

To get the percentage you can:

• take the total number of employees out there (in a subquery for example: `SELECT COUNT(*) FROM empleado` )
• multiply that total by 100
• divide the total of rows among the total of employees by 100

The query would look like this:

``````SELECT
COUNT(*) AS Total,
t.descripcion,
( COUNT(*) / (SELECT COUNT(*) FROM empleado)*100 ) '%'
INNER JOIN tipo_sanguineo t ON e.tipo_sangre=t.id_tipo
GROUP BY e.tipo_sangre;
``````

Exit:

``````Total   descripcion    %
-------------------------------
1       A+             33,3333
1       A-             33,3333
1       B+             33,3333
``````