0

我对 PL/SQL 非常“菜鸟”,但我必须专门编写一些 PL/SQL 代码(DECLARE/BEGIN/等),而不仅仅是一个查询。

我真的在努力寻找一个很好的例子,希望能让我掌握其余的问题。这是我目前正在尝试弄清楚的一个:

编写一个 PL/SQL 程序来计算每个学生的总学分。注意:学生获得 F 的课程不计入学分。如果学生重修一门他/她成绩较低的课程,则只计入一次学分。输出应包括学生人数和总学分。

Student-schema =(studentnum, name, standing, gpa, major)
Class-schema = (schedulenum, semester, department, classnum, days, time, place, enrollment)
Instructor-schema = (name, department, office)
Teaches-schema = (name, schedulenum, semester)
Taking-schema = (studentnum, schedulenum, semester, grade)

这是我可悲的尝试之一:

DECLARE
    stud_id student.studentnum%TYPE;
    total number(10);
BEGIN
    FOR count IN (SELECT* FROM taking)
    LOOP
        SELECT studentnum, count(*) total
    INTO stud_id, total
    WHERE grade >= 1;
    END LOOP;
END; 
/

在上面的一个中,idk 我在做什么错或对。我在第 1 行收到错误“精确提取返回的行数超过请求的行数”。我之前遇到过这个错误,并认为 for 循环可以解决它。显然我错了。我什至不知道这将如何成功输出结果,即使它确实查询正确。我很迷茫,一点点的方向会有很长的路要走。

4

3 回答 3

1

下面的代码迭代学生的结果集和他们的成绩计数,不计算“F”成绩。该片段没有实施任何课程不得贡献超过一个成绩的限制;但是,它应该让你开始,这样你就可以补充缺失的部分。

DECLARE
    stud_id student.studentnum%TYPE;
    total number(10);
BEGIN
    FOR i IN (
        SELECT s.studentnum  stud
             , COUNT(*)      cnt
          FROM student  s
          JOIN taking   t   on ( t.studentnum   = s.studentnum  )
          JOIN class    c   on ( c.schedulenum  = t.schedulenum )
         WHERE t.grade > 0
      GROUP BY s.studentnum
    ) LOOP
        NULL; -- whatever
    END LOOP;
END; 
/
于 2013-04-23T16:40:30.787 回答
0

I do believe I found a proper solution. None of the answers really worked, so I ended up just making a cursor to go through the data. Because of the joins I'm doing, I believe it will filter out duplicate classes taken by the same student:

DECLARE
    CURSOR c IS
        SELECT taking.studentnum, count(*) total
        FROM student, taking, class
        WHERE grade > 0
    AND student.studentnum = taking.studentnum
    AND taking.schedulenum = class.schedulenum
    AND taking.semester = class.semester
    GROUP BY taking.studentnum;
    credits c%ROWTYPE;
BEGIN
    OPEN c;
    LOOP
    FETCH c INTO credits;
        EXIT WHEN c%NOTFOUND;
    dbms_output.put_line('Student#: ' || credits.studentnum || 
        '  Credits: ' || credits.total);
    END LOOP;
END;
/
于 2013-04-23T19:21:46.363 回答
0

我认为select * from taking不会返回数字,而是返回一堆行。如果你想要你需要的行数 SELECT count(*) FROM taking

于 2013-04-23T16:32:12.343 回答