문제

I'm trying to calculate the GPA of each student in between 4 tables:

Student(STUDENT_ID, STUDENT_LNAME, STUDENT_FNAME, MAJOR)
Course(COURSE_NO, COURSE_NAME, DEPT_CODE, CREDITS)
Grade(COURSE_NO, STUDENT_ID, GRADE)
Grade-point(GRADE, POINTS)

This is the code I've written:

    SELECT S.STUDENT_ID, S.STUDENT_LNAME, S.STUDENT_FNAME, 
    SUM(C.CREDITS*P.POINTS)/SUM(C.CREDITS) AS GPA
    FROM GRADE_POINT P, STUDENT S, COURSE C, GRADE G
    WHERE S.STUDENT_ID=G.STUDENT_ID
    AND C.COURSE_NO=G.COURSE_NO
    AND G.GRADE=P.GRADE
    GROUP BY S.STUDENT_ID, S.STUDENT_LNAME, S.STUDENT_FNAME, GPA;

I haven't been taught yet the CREATE PROCEDURE query, therefore I'm not supposed to use it at this level. However, the code doesn't work. It says GPA is an invalid identifier. I honestly can't tell how it's wrong. I also tried removing as many functions as possible for the variable, like this:

    SELECT S.STUDENT_ID, S.STUDENT_LNAME, S.STUDENT_FNAME, (C.CREDITS*P.POINTS) AS 
    TOT_CR_PT, SUM(C.CREDITS) AS TOT_CREDIT, (TOT_CR_PT/TOT_CREDIT) AS GPA

But the problem still exists with GPA. What's the problem and how can I fix this?

도움이 되었습니까?

해결책

You don't want to be grouping by GPA, as this is the alias you've given to the SUM() Aggregate that you've applied to the set that you have grouped on (Student). Just remove it from the GROUP BY:

SELECT S.STUDENT_ID, S.STUDENT_LNAME, S.STUDENT_FNAME, 
SUM(C.CREDITS*P.POINTS)/SUM(C.CREDITS) AS GPA
FROM GRADE_POINT P, STUDENT S, COURSE C, GRADE G
WHERE S.STUDENT_ID=G.STUDENT_ID
AND C.COURSE_NO=G.COURSE_NO
AND G.GRADE=P.GRADE
GROUP BY S.STUDENT_ID, S.STUDENT_LNAME, S.STUDENT_FNAME;

One other point which might get you marks is to use JOIN instead of (ab)using the WHERE clause (recent versions of Oracle do support this syntax):

FROM STUDENT S 
     INNER JOIN GRADE G
     ON S.STUDENT_ID=G.STUDENT_ID
...
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top