现在,我有一个查询可以获得3列。
SELECT a.studentID,a.classdetailID,c.course_title
FROM studentcoursedetails a,classdetails b,course c
WHERE b.classdetailID = a.classdetailID
AND b.courseID = c.courseID
ORDER BY c.course_title,b.classdetailID
我得到的是一个列,其中显示了完成课程的学生的ID,他们所在班级的ID以及课程本身的标题。这些方面的东西:
5---8----Airline Security Operations
4---8----Airline Security Operations
6---8----Airline Security Operations
4---9----Airline Security Operations
8---10---Airline Security Operations
5---10---Airline Security Operations
1---1----Airport Design and Construction
4---1----Airport Design and Construction
2---1----Airport Design and Construction
1---2----Airport Design and Construction
我需要的是一张能够显示以下内容的表格:
course_title Number of Classes Number of students
Airline Security Operations 3 4
我想过使用CASE
,但是当我想到它时,我只是迷路了。我很感激你的帮助。
答案,感谢gbn:
SELECT c.course_title,COUNT(DISTINCT a.studentID)AS“课程中的学生”,COUNT( DISTINCT a.classdetailID)AS“总课程”
FROM studentcoursedetails a JOIN classdetails b ON b.classdetailID = a.classdetailID
加入课程c ON b.courseID = c.courseID
GROUP BY c.course_title
答案 0 :(得分:3)
您希望按course_title
与此类似(使用SQL Server列别名语法),具有正确的JOIN语法
SELECT
c.course_title
,COUNT(DISTINCT a.studentID) AS [Number of students that have taken the course]
,COUNT(DISTINCT a.classdetailID) AS [Number of Classes]
FROM
studentcoursedetails a
JOIN
classdetails b ON b.classdetailID = a.classdetailID
JOIN
course c ON b.courseID = c.courseID
GROUP BY
c.course_title