usr021986
usr021986

Reputation: 3511

Mysql query for grouping data using two conditions

I have below data table and need to accomplish the result for Student count as per availability and per department

enter image description here Note: Different Department may come in later.

enter image description here

Want to have result as below: (Student count as per availability and per department) enter image description here

Upvotes: 0

Views: 35

Answers (2)

D-Shih
D-Shih

Reputation: 46249

First you need to get count by department,studavailablity on a subquery.then use GROUP_CONCAT function to create the studCount by subquery.

If you want to show Yes in front of No you could Add order by on GROUP_CONCAT function.

You can try this.

SELECT department,GROUP_CONCAT(Concat(studavailablity,' ',c) order by 1 DESC)  'count'
FROM(
  SELECT count(1) c,department,studavailablity 
  FROM T
  GROUP BY studavailablity,department
) t
group by department
order by department desc

sqlfiddle:http://sqlfiddle.com/#!9/c4e56f/16

Upvotes: 1

Brayden
Brayden

Reputation: 201

How about SELECT Department, Studavailability, COUNT(*) FROM students GROUP BY Department, Studavailability;? Best of luck!

Upvotes: 0

Related Questions