假设我有下表:
Student Course University
1 a x
1 b x
1 c x
1 a y
2 a x
2 a y
2 a z
3 a x
对于每个学生,我都在努力寻找他们注册的独特课程和大学的数量。
输出如下:
Student No. of Courses No. of Universities
1 3 2
2 1 3
3 1 1
我如何为此构建SQL?
答案 0 :(得分:10)
SELECT Student,
COUNT(DISTINCT Course) AS NumberOfCourses,
COUNT(DISTINCT University) AS NumberOfUniversities
FROM YourTable
GROUP BY Student