Pace Wasden
Pace Wasden

Reputation: 35

Division and Ratios in SQl

This gives me what I am looking for just showing how many Males and Females. I am trying to get the ratio of Males/Females. Is there any easy way to do that? And have it show two digits after the decimal?

select gender,count (*)

from HumanResources.Employee

group by gender

Upvotes: 1

Views: 115

Answers (2)

hoangnh
hoangnh

Reputation: 249

Sound like you want

select gender, count(*), 
cast(count(*)*100 ::float/ (select count(*) from HumanResources.Employee)::float as decimal(5,2))
from HumanResources.Employee
group by gender

Upvotes: 0

danblack
danblack

Reputation: 14666

Leaving formatting to an application level problem:

SELECT sum(gender='Males')/sum(gender='Females')
FROM HumanResources.Employee

gender=X is a boolean expression, either 1 (true) or 0 (false) is why this works.

Obvious this misses the non-male/female emploees.

Upvotes: 2

Related Questions