for example ,i have table represents teachers in school including his/her name , sex , class he/she belongs to.i want know , each class, how many males , how many females teachers in sql statement.
abosolutely , each teacher can belong 1 class!
column type description class number class number,e.g. 0,1,2,3 ... sex number teacher sex, 0 represents male , 1 represents female. teacher varchar teacher name lily,lucy
more deep:
i have table represents teachers in school including his/her name , age , class he/she belongs to.i want know , each class, how many teachers younger 20 , how many teachers older 20 @ same time in sql statement.
column type description class number class number,e.g. 0,1,2,3 ... age number teacher ages. teacher varchar teacher name lily,lucy
try :
select class, sum(male) male, sum(female) female, sum(younger) younger, sum(older) older from( select class, (case when sex = 0 1 else 0 end) male, (case when sex = 1 1 else 0 end) female, (case when age < 20 1 else 0 end) younger, (case when age >= 20 1 else 0 end) older yourtable ) t group class
Comments
Post a Comment