2016-12-07 131 views
0
DECLARE 
    CURSOR cursor1 IS SELECT STUDENTNAME, COURSEID, COURSEDESCRIPTION, COURSECREDITS, GRADE 
    FROM STUDENTINFO 
    WHERE STUDENTNAME = '&student' 
    GROUP BY (STUDENTNAME, COURSEID, COURSEDESCRIPTION, COURSECREDITS, GRADE) 
    ORDER BY COURSEID; 
    S_NAME STUDENTINFO.STUDENTNAME%TYPE; 
    S_COURSEID STUDENTINFO.COURSEID%TYPE; 
    S_COURSEDESCRIPTION STUDENTINFO.COURSEDESCRIPTION%TYPE; 
    S_COURSECREDITS STUDENTINFO.COURSECREDITS%TYPE; 
    S_GRADE STUDENTINFO.GRADE%TYPE; 
    S_TOTALCREDITS STUDENTINFO.COURSECREDITS%TYPE := 0; 
    S_A STUDENTINFO.COURSECREDITS%TYPE := 4; 
    S_B STUDENTINFO.COURSECREDITS%TYPE := 3; 
    S_C STUDENTINFO.COURSECREDITS%TYPE := 2; 
    S_D STUDENTINFO.COURSECREDITS%TYPE := 1; 
    S_F STUDENTINFO.COURSECREDITS%TYPE := 0; 
    S_GPABEFOREDIV STUDENTINFO.COURSECREDITS%TYPE := 0; 
    S_CREDITSTOGPA STUDENTINFO.COURSECREDITS%TYPE := 0; 
    S_GPA NUMBER(2); 
BEGIN 
    OPEN CURSOR1; 
    LOOP 
    FETCH CURSOR1 INTO S_NAME, S_COURSEID, S_COURSEDESCRIPTION, S_COURSECREDITS, S_GRADE; 
    EXIT WHEN cursor1%NOTFOUND; 
     IF (S_GRADE = 'A') THEN 
     S_CREDITSTOGPA := (S_COURSECREDITS * S_A); 
     S_GPABEFOREDIV := S_GPABEFOREDIV + S_CREDITSTOGPA; 
    ELSIF (S_GRADE = 'B') THEN 
     S_CREDITSTOGPA := (S_COURSECREDITS * S_B); 
     S_GPABEFOREDIV := S_GPABEFOREDIV + S_CREDITSTOGPA; 
    ELSIF (S_GRADE = 'C') THEN 
     S_CREDITSTOGPA := (S_COURSECREDITS * S_C); 
     S_GPABEFOREDIV := S_GPABEFOREDIV + S_CREDITSTOGPA; 
    ELSIF (S_GRADE = 'D') THEN 
     S_CREDITSTOGPA := (S_COURSECREDITS * S_D); 
     S_GPABEFOREDIV := S_GPABEFOREDIV + S_CREDITSTOGPA; 
    ELSE 
     S_CREDITSTOGPA := (S_COURSECREDITS * S_F); 
     S_GPABEFOREDIV := S_GPABEFOREDIV + S_CREDITSTOGPA; 
    END IF; 
    DBMS_OUTPUT.PUT_LINE(CHR(10)); 
    DBMS_OUTPUT.PUT_LINE(S_COURSEID ||' '|| S_COURSEDESCRIPTION ||' '|| S_COURSECREDITS ||' '|| S_GRADE); 
    S_TOTALCREDITS := S_TOTALCREDITS + S_COURSECREDITS; 
    S_GPA := ROUND((S_GPABEFOREDIV/S_TOTALCREDITS),2); 
    END LOOP; 
    DBMS_OUTPUT.PUT_LINE('Total Credits: ' || S_TOTALCREDITS); 
    DBMS_OUTPUT.PUT_LINE('Overall GPA: ' || S_GPA); 
    CLOSE CURSOR1; 
    END; 
    /

我需要將S_GPA舍入到2位小數。我試圖在多個景點,如在在PLSQL中舍入

Overall GPA: ROUND(S_GPA,2) 

使用ROUND函數2點,但我還是堅持了0小數

我必須這樣做在SELECT語句中不知何故?任何和所有的幫助表示讚賞。

+4

您確定您的實際GPA計算不會導致整數?如果確實如此,並且仍然需要GPA顯示兩位小數(如3.00),則不要簡單地在輸出行末尾連接S_GPA;將其包裹在'to_char(...,'9.00')'中。 – mathguy

+0

'S_GPA'的類型是什麼? – GurV

+0

CourseCredits是一個數字,等級是VARCHAR2 – Josh

回答

0

哦,我知道了,我將S_GPA更改爲S_GPA(4,2)。