ARK
ARK

Reputation: 4054

Count Query on same Column

Following are two SQL tables :

I want to count of each gender in each classroom. I expect Query results as:

Classroom | CountMale | CountFemale
          |           |
          |           |
          |           |

How can I get the count?

Upvotes: 0

Views: 61

Answers (2)

rafalopez79
rafalopez79

Reputation: 2076

Maybe something like this:

SELECT Classroom, 
      SUM(CASE WHEN GENDER = 'MALE' THEN 1 ELSE 0 END) AS CountMale,
      SUM(CASE WHEN GENDER = 'FEMALE' THEN 1 ELSE 0 END) AS CountFemale
FROM TABLE
GROUP BY Classroom

Change the literals.

Upvotes: 0

Mihai
Mihai

Reputation: 26784

SELECT Classroom,
SUM(CASE WHEN Gender='male' THEN 1 ELSE 0 END) as CountMale,
SUM(CASE WHEN Gender='female' THEN 1 ELSE 0 END) as CountFemale
FROM t
GROUP BY Classroom

Upvotes: 1

Related Questions