I have data in database like this
id class gender 1 A F 2 B F 3 A M 4 A F 5 A M 6 B M 7 A F
From this data I want to make select statement to produce report like this
_________________________
Gender
class M F Total
_________________________
A 2 3 5
B 1 1 2
_________________________
TOTAL 3 4 7
How can I make that select statement ?
Have a look at the following example
SELECT class,
SUM(CASE WHEN gender = 'M' THEN 1 ELSE 0 END) `M`,
SUM(CASE WHEN gender = 'F' THEN 1 ELSE 0 END) `F`,
COUNT(1) Total
FROM Table1
GROUP BY class
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With