Something like this should work:
SELECT
school,
SUM(CASE WHEN status = 'fail' THEN 1 ELSE 0 END) as [fail],
SUM(CASE WHEN status = 'pass' THEN 1 ELSE 0 END) as [pass]
FROM Students
GROUP BY school
ORDER BY school
EDIT
Almost forgot, but you can also write a query this way:
SELECT
school,
COUNT(CASE WHEN status = 'fail' THEN 1 END) as [fail],
COUNT(CASE WHEN status = 'pass' THEN 1 END) as [pass]
FROM Students
GROUP BY school
ORDER BY school
I am not sure if there is a performance advantage in the second query. My guess would be if it were probably very small. I prefer using the first query because I think it is clearer, but both should work. Also, I don't have an instance of MySql to test, but according to @Johan, ORDER BY clauses are not needed.
source
share