算在一个计数内

时间:2010-11-28 07:45:33

标签: sql oracle count

修订版 好的,多亏了你的所有意见,我弄清楚我做错了什么(抱歉的家伙)。我抓住了课程ID,它被视为一个不是实际课程的部分。为了获取实际的课程名称,我必须转到课程表。

所以现在我有来自Enrollment的StudentID和来自Course的CourseNum需要用来计算。我会尝试和你们提供的东西一起工作,看看我能否拿出结果。

编辑: 这是我修改过的SQL。这为我提供了每个学生的总课程。我到了那里:

SELECT     Count(DISTINCT Course.courseNum), Grades.studentID
FROM         Grades INNER JOIN
             Course ON Grades.courseID = Course.courseID
             GROUP BY Grades.studentID;

最终守则,以防万一关心:

SELECT COUNT(NumCourses) FROM 
(SELECT     Count(DISTINCT Course.courseNum)AS NumCourses
FROM         Grades INNER JOIN
                      Course ON Grades.courseID = Course.courseID
                     GROUP BY Grades.studentID
           HAVING Count(DISTINCT Course.courseNum) = 1) a;

6 个答案:

答案 0 :(得分:3)

试试这个:

select count(studentId), count(courseId) from enrolment group by courseId having (count(courseId) = 2);

答案 1 :(得分:1)

要获得4门以上课程的学生总数,这就是SQL:

SELECT  COUNT(CourseCount) AS CourseCount
FROM    (
    SELECT  StudentID, COUNT(CourseID) AS CourseCount
            FROM enrollment
            GROUP BY StudentID
            HAVING  (COUNT(CourseID) >= 4)) AS T

使用第二个查询获得学生计数要简单得多。

答案 2 :(得分:0)

SELECT COUNT(*) FROM
    (SELECT COUNT(*) FROM enrollment 
     GROUP BY studentid HAVING COUNT(*) = 4)

答案 3 :(得分:0)

SELECT COUNT(NumCourses) AS NumStudents, NumCourses FROM (SELECT COUNT(courseID) AS NumCourses FROM enrollment GROUP BY courseID HAVING COUNT(courseID) = 4) As SomeTableAlias

答案 4 :(得分:0)

我认为你想要参加四门课程的学生人数。 如果您想计算学生人数和课程数量,请执行以下操作:

SELECT * FROM (SELECT COUNT(*) AS NumStudents FROM (SELECT DISTINCT studendid FROM enrollment)), (SELECT COUNT(*) AS NumCourses FROM (SELECT DISTINCT courseid FROM enrollment))

答案 5 :(得分:0)

select count(*)
  from (select student_id from enrollment group by student_id having count(*) = 4)

内部查询为您提供了参加4门课程的学生的ID,然后我会计算出来。