MySQL GROUP BY not giving desired result - mysql

I have to show summary as number of males, number of females in each unit. Tried the following query:
SELECT
unit,
CASE gender
WHEN 'Male' THEN COUNT(id)
END AS 'male',
CASE
WHEN gender = 'Female' THEN COUNT(id)
END AS 'female'
FROM
admission_info
WHERE
admission_date = '2017-04-15'
GROUP BY gender
Here's the Fiddle which shows the output. But i want it in 1 row where it should show unit name, number of males, number of females.

SELECT unit,
sum(CASE gender WHEN 'Male' THEN 1 else 0 END) AS 'male',
sum(CASE WHEN gender='Female' THEN 1 else 0 END) AS 'female'
FROM admission_info WHERE admission_date='2017-04-15'
group by unit

SELECT unit, gender, count(id) as count
FROM admission_info WHERE admission_date='2017-04-15' GROUP BY unit, gender

Related

need absent and present count with month name

I need a month name with absent and present count. This is my database query:
SELECT sid,COUNT(CASE WHEN STATUS ='A' THEN 1 END) AS absent_count,COUNT(CASE WHEN STATUS ='P' THEN 1 END) AS present_count,
MONTHNAME(attendance_date) AS `Month_Name`
FROM attendance
WHERE SID = '2'
AND campus_id = 2
GROUP BY sid;
There's no point in group by sid - it will always be '2', as per your where clause. Instead, since you want to count per month name, that should appear in the group by clause:
SELECT MONTHNAME(attendance_date) AS `Month_Name`,
COUNT(CASE WHEN STATUS ='A' THEN 1 END) AS absent_count,
COUNT(CASE WHEN STATUS ='P' THEN 1 END) AS present_count,
FROM attendance
WHERE sid = '2' AND campus_id = 2
GROUP BY MONTHNAME(attendance_date);

RETURN the amount of males and the amount of females in 1 table using MySQL

I need to RETURN the amount of males and the amount of females in 1 table using MySQL. I have created a query that return a tables with males and females rows, but my column is not being populated. its results =0;
Here is my query. I get the table but it doesn't get populated
SELECT COUNT(gender) AS 'Female', COUNT(gender) AS 'Male'
FROM customers
WHERE gender = 'female' AND 'male';
Any Suggestions,
This should work:
SELECT
SUM(IF(gender = 'female', 1, 0)) AS 'Female',
SUM(IF(gender = 'male', 1, 0)) AS 'Male'
FROM customers
The IF gets you a value of 1 or 0 depending on whether the gender is female (resp. male) or not, and then you just sum up those zeros and ones to get the overall count.
You can do the CASE statement too:
SELECT
SUM(CASE WHEN gender = 'female' THEN 1 ELSE 0 END) AS 'Female',
SUM(CASE WHEN gender = 'male' THEN 1 ELSE 0 END) AS 'Male'
FROM customers
Since MySQL equates true with 1 and false with 0 you can use a shorter version:
SELECT
SUM(gender = 'female') AS `Female`,
SUM(gender = 'male') AS `Male`
FROM customers
SQL FIDDLE

SQL using CASE in count and group by

I'm using CASE to categorize data in the table and count them but the results aren't accurate
live demo [here]
select DATE(date) as day, count(*),
count(distinct case when name = 'fruit' then 1 else 0 end) as fruits,
count(distinct case when name = 'vege' then 1 else 0 end) as vege,
count(distinct case when name = 'sweets' then 1 else 0 end) as sweets
from food
group by day
with rollup
I'm not sure if the issue is with CASE or in the string matching = because there's no 'sweets' still it counts 1?
any pointers I'd be grateful
Your problem is that COUNT counts every result that is not NULL. In your case you are using:
COUNT(distinct case when name = 'sweets' then 1 else 0 end)
So, when the name is not sweets, it counts the 0. Furthermore, since you are using DISTINCT, it counts just one or two values. You should either use SUM or remove the DISTINCT and the ELSE 0:
SELECT DATE(date) as day,
COUNT(*),
SUM(CASE WHEN name = 'fruit' THEN 1 ELSE 0 END) as fruits,
SUM(CASE WHEN name = 'vege' THEN 1 ELSE 0 END) as vege,
SUM(CASE WHEN name = 'sweets' THEN 1 ELSE 0 END) as sweets
FROM food
GROUP BY DAY
WITH ROLLUP
Or:
SELECT DATE(date) as day,
COUNT(*),
COUNT(CASE WHEN name = 'fruit' THEN 1 ELSE NULL END) as fruits,
COUNT(CASE WHEN name = 'vege' THEN 1 ELSE NULL END) as vege,
COUNT(CASE WHEN name = 'sweets' THEN 1 ELSE NULL END) as sweets
FROM food
GROUP BY DAY
WITH ROLLUP
Here is a modified sqlfiddle.
You can't group by an alias. You have to group by the expression.
group by date(date)
You can group on an Alias:
SELECT
FROM_UNIXTIME(UnixTimeField, '%Y') AS 'Year'
,FROM_UNIXTIME(UnixTimeField, '%m') AS 'Month'
FROM table p
GROUP BY Year, Month

Group By results as individual column names

How to get the groupby result as column names
if staff table is there when we use
select count(*) from staff group by gender; gives me as
but i need them as columns Male | Female | None
select sum(case when gender = 'Male' then 1 else 0 end) as 'Male',
sum(case when gender = 'Female' then 1 else 0 end) as 'Female',
sum(case when gender not in ('Male','Female') or gender is null then 1 else 0 end) as 'None'
from staff;

MYSQL count function merging

In my database table I have two columns that hold either 0 or 1.
I have type and Gender, where type means 0 => teacher and 1 => student and for gender: 0 => male and 1 => female.
How can I write a single sql query to get number of teachers, students, males and females?
Right now I have:
select COUNT(type) as teachers from my_table where type = 0; // Teachers
select COUNT(type) as students from my_table where type = 1; // Students
select COUNT(gender) as males from my_table where type = 0; // Males
select COUNT(gender) as females from my_table where type = 1; // Females
Can it be done in one query? If so, how?
You can use CASE for that using SUM function:
SELECT SUM(CASE type WHEN 1 THEN 1 ELSE 0 END) AS students,
SUM(CASE type WHEN 0 THEN 1 ELSE 0 END) AS teachers,
SUM(CASE gender WHEN 1 THEN 1 ELSE 0 END) AS females,
SUM(CASE gender WHEN 0 THEN 1 ELSE 0 END) AS males
FROM my_table;
You can also use COUNT function instead of SUM like this:
SELECT COUNT(CASE type WHEN 1 THEN 1 ELSE NULL END) AS students,
COUNT(CASE type WHEN 0 THEN 1 ELSE NULL END) AS teachers,
COUNT(CASE gender WHEN 1 THEN 1 ELSE NULL END) AS females,
COUNT(CASE gender WHEN 0 THEN 1 ELSE NULL END) AS males
FROM my_table;
See this SQLFiddle
This way you can do it in a single query. If you have only two types of data in your table then you don't need to specify IN conditions in WHERE clause:
SELECT SUM(IF(type = 1, 1, 0)) as students,
SUM(IF(type = 0, 1, 0)) as teachers,
SUM(IF(gender = 1, 1, 0)) as females,
SUM(IF(gender = 0, 1, 0)) as males
FROM my_table
WHERE type IN(0,1)
AND gender IN(0,1);
You could achive this using subqueries.
SELECT COUNT(type) AS students,
(SELECT COUNT(type) FROM my_table WHERE type = 0) As teachers,
(SELECT COUNT(gender) FROM my_table WHERE gender = 1) AS females,
(SELECT COUNT(gender) FROM my_table WHERE gender = 0) AS males
FROM my_table WHERE type = 1;
Yes, you can. Did you try taking each of your SQL statements and putting them together?
SQL1 as fld1, SQL2 as fld2, someOtherFieldsIfNeeded FROM ...