如何计算学生姓名并显示所有数据
ID Name Subject
1 Jack English
2 Tiffany English
3 Ben Mathematics
4 Tiffany Science
5 Jack Mathematics
然后输出结果的返回将显示每个人的主题。
ID Name Subject [Subject Taken]
1 Jack English 2
2 Tiffany English 2
3 Ben Mathematics 1
4 Tiffany Science 2
5 Jack Mathematics 2
答案 0 :(得分:2)
你可以通过子查询实现这一点,但不能没有一个组。首先创建一个组,然后将该组与原始表一起加入。
按结果划分的子查询组
Name CountOfSubject
Ben 1
jack 2
Tiffany 2
-
SELECT SubQuery.Name, Table1.Subject, SubQuery.CountOfSubject
FROM
( SELECT Table1.Name,
Count(Table1.Subject) AS CountOfSubject
FROM Table1
GROUP BY Table1.Name) AS SubQuery
INNER JOIN Table1 ON SubQuery.Name = Table1.Name;
结果
Name Subject CountOfSubject
jack English 2
Tiffany English 2
Ben Mathematics 1
Tiffany Science 2
jack Mathematics 2