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
How to Get Column Attributes Query from Table Name Using Postgresql
Make All Store Images the Base, Small and Thumbnail Images in Magento
Which Is Better: Bookmark/Key Lookup or Index Seek
Creating a Udf(User Define Function) If Is Does Not Exist and Skipping It If It Exists
SQL Server Management Studio 2012 - Export All Tables of Database as CSV
How to Schedule a SQL Job in Microsoft Azure SQL Database
Are Left Outer Joins Associative
Copy Data Between Two Server Instances
Which Lock Hints Should I Use (T-Sql)
Fastest Way Merge Two SQLite Databases
How to Get Multiple Rows into One Line as a String
Store Multiple Elements in JSON Files in Aws Athena
SQL Server Filestream Limitation
Inline Blob/Binary Data Types in SQL/Jdbc
MySQL Nested Sets - How to Find Parent of Node
Teradata SQL Pivot Multiple Occurrences into Additional Columns