Count Male, Female and Total

I have a column called Gender, how can I count male and female group by division

You want a conditional aggregation

SELECT Division
, SUM(case when Gender = 'Male' then 1 end) as 'Total Male'
, SUM(case when Gender = 'Female'then 1 end) as 'Total Female'
FROM Table
GROUP by Division

Count number of male and female

SELECT SUM(CASE WHEN Gender = 'male' THEN 1 ELSE 0 END) as Male_count,
SUM(CASE WHEN Gender = 'Female' THEN 1 ELSE 0 END) as Female_count
FROM some_unknown_table

As nightfox79 mentioned, if your default collation is case sensitive, you probably should transform the text to ensure it would match.
So, use this

UPPER(GENDER) = 'MALE'
UPPER(GENDER) = 'FEMALE'

Sidenote: I think you should use a tinyint column for Gender following this format, as Damien mentioned. https://en.wikipedia.org/wiki/ISO/IEC_5218

i want to count total number of males and females from the result of sub query. Is there any mysql query

select count(*) as total,
sum(gender = 'Female') as females,
sum(gender = 'Male') as males
FROM table1
INNER JOIN table2 ON table1.id = table2.form_id
WHERE table1.city = 'Banglore'

If you want to put all that together this would be an ugly query for MySQL with versions below 8.0. But with MySQL 8+ you could simply do

select name, gender, 
count(*) over() as total,
sum(gender = 'Male') over() as males,
sum(gender = 'Female') over() as females
FROM table1
INNER JOIN table2 ON table1.id = table2.form_id
WHERE table1.city = 'Banglore'

Count Male and Female in SQL and how many records are there. With male listed with their name

Try This but please keep in mind that STRING_AGG is not available on every DBMS system

select Gender, count(*) Count, String_agg(name, ',')
from t
group by Gender

total of male and female in every year level and then all using codeigniter

Assuming you have Yearlevel as int datatype...then you can use

function gendercountzx()
{
$query = $this->db->query('SELECT CONCAT("Grade ", Yearlevel) as Yearlevel, COUNT(*) AS studentcount, sum(case when Sex = "Male" then 1 else 0 end) AS malecount, sum(case when Sex = "Female" then 1 else 0 end) AS femalecount FROM studentinformation GROUP BY Yearlevel ORDER BY Yearlevel DESC');
return $querys->result();
}


Related Topics



Leave a reply



Submit