将结果合并为一行

时间:2018-03-26 20:27:11

标签: sql

如何在SQL中将这些结果显示在一行?

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以使用GROUP BY执行此操作:

SELECT school_id, school_name, grade, SUM(CASE WHEN Gender='F' THEN 1 ELSE 0 END) as female, SUM(CASE WHEN Gender='M' THEN 1 ELSE 0 END) as male
FROM yourtable
GROUP BY school_id, school_name, grade;