无法在SQL Server中使用分组依据

我正在学习如何Group By在SQL Server中使用,并且正在尝试编写一个查询,该查询将使我能够以数字的形式从校友那里获取所有信息。

我的桌子如下:

 Name | Alumn_ID | Course | Credits | Passed
 Peter    1         Math       2        YES
 John     2         Math       3        YES
 Thomas   3         Math       0        NO
 Peter    1        English     3        YES
 Thomas   2        English     2        YES
 John     3        English     0        NO

我想要的结果是以下之一:

Alumn | Total_Credits | Courses | Passed | Not_Passed
Peter        5             2         2          0
John         5             2         2          0
Thomas       0             2         0          2

我知道我必须使用Group ByCOUNT但是由于我是一个初学者,所以我陷入了困境,我真的不知道该如何从表的“通过”列中分离出来Passed以及Not_Passed结果,在此先感谢

高棉
SELECT t.id, t.name AS alum,
SUM(credits) AS total_credits,
COUNT(*) AS courses,
SUM(CASE WHEN Passed = 'YES' THEN 1 ELSE 0 END) AS Passed,
SUM(CASE WHEN Passed = 'NO' THEN 1 ELSE 0 END) AS Reprobated
FROM t
GROUP BY t.id, t.name

我认为未通过的手段没有通过。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章