have two three tables one for student, second for class, and the third for gender. I am trying to get the total number of each gender and the percentage. I used the following statment to get the number and it works well:
SELECT Gender.GenderName as Gender, COUNT(*) as cnt
FROM (Client INNER JOIN
Gender ON Student.GenderID = Gender.GenderID)
GROUP BY Gender.GenderName
I could not figure out how I can get the percenage, also how to make ClassName or ID as as selectable item to get the gender for each class or all classes by using @ClassId int.
I don’t have access to a SQL Server right now but you use either
CastorConvertto convert to a floating type. Or you may want to refer to your SQL Server’s documentation for its specific type conversion functions.PS: Thanks for pointing out my mistake.
a.cnt/b.cntwould do integer math and return zero if b.cnt > a.cnt (In this case it would be) … we need to convert eithera.cntorb.cntto a float soa.cnt/b.cntbecomes a float and then times it by 100 to give you the percent. Also, I missed that the GenderName had been aliased in the inner SELECT.