我不太确定我需要解决这个问题,即。这是子查询,分组依据,联合/加入问题,还是其他完全......
我有一个学生成绩表设置如下:
student gender class result
------- ------ ----- ------
Bob M Math A+
Mary F Math A+
Peter M Math A+
Jane F Math B
我想要做的是能够看到按类汇总的结果,但要看到性别差异。例如(基于上面的示例表):
males females class result
----- ------- ----- ------
2 1 Math A+
0 1 Math B
我希望我的问题有道理,任何帮助都会受到赞赏!
答案 0 :(得分:7)
SELECT SUM(CASE
WHEN Gender = 'M' THEN 1
ELSE 0
END) AS Males
, SUM(CASE
WHEN Gender = 'F' THEN 1
ELSE 0
END) AS Females
, class
, result
FROM student
GROUP BY class, result
答案 1 :(得分:0)
SELECT CLASS,
RESULT,
SUM(CASE WHEN GENDER ='M' THEN 1 ELSE 0 END) MALE ,
SUM(CASE WHEN GENDER ='F' THEN 1 ELSE 0 END) FEMALE
FROM TABLE_1
GROUP BY CLASS,RESULT