Total of Male and Female in Grade 8 using sql, codeigniter

I have a problem about total in Yearlevel like total number of male and female in grade 7: enter image description here

I can count all by using codeigniter:

  <h3> Total Students: <?php echo  $this->db->count_all('studentinformation'); ?></h3>

And then group them by using this:

SELECT Yearlevel, Sex, COUNT(Id) AS NumGender
FROM studentinformation
GROUP BY Sex, Yearlevel

My problem now is count in yearlevel total of grade7, grade 8 and then arrage them

Upvotes: 0

Views: 180

Answers (1)

jarlh
jarlh

Reputation: 44786

Perhaps this is what you're looking for?

Use case expressions to do conditional counting:

SELECT Yearlevel,
       COUNT(*) AS studentcount,
       COUNT(case when Sex = 'Female' then 1 end) AS femalecount,
       COUNT(case when Sex = 'Male' then 1 end)   AS malecount
FROM studentinformation
GROUP BY Yearlevel

Upvotes: 1

Related Questions