假设我有下表:
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
对于每个学生,我试图找到他们注册的独特类(class)和大学的数量。
输出如下:
Student No. of Courses No. of Universities
1 3 2
2 1 3
3 1 1
我将如何为此构建 SQL?
最佳答案
SELECT Student,
COUNT(DISTINCT Course) AS NumberOfCourses,
COUNT(DISTINCT University) AS NumberOfUniversities
FROM YourTable
GROUP BY Student
关于用于查找每个用户的计数的 SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14247052/