Andrea,
        Here is a version of the function you asked for, IT IS NOT WELL
TESTED, should give you something to go on.

Scott Crabtree

FUNCTION calc_avg (student_id IN NUMBER)
   RETURN NUMBER
AS
   CURSOR student_c
   IS
      SELECT      snum
               || call# class, credit, RTRIM (grade) grade,
                 credit
               * DECODE (RTRIM (grade), 'A', 4, 'B', 3, 'C', 2, 'D', 1, 'F',
0) weight
          FROM enrollment
         WHERE student# = student_id
      ORDER BY grade DESC;

   last_class    NUMBER              := 0;
   last_weight   NUMBER              := 0;
   last_credit   NUMBER              := 0;
   last_grade    VARCHAR2 (1)        := 'Z';
   cum_weight    NUMBER              := 0;
   cum_credits   NUMBER              := 0;
   gpa           NUMBER (5, 2)       := 0.00;
   student_rec   student_c%ROWTYPE;
BEGIN
   OPEN student_c;
   FETCH student_c INTO student_rec;

   WHILE student_c%FOUND
   LOOP
      --DBMS_OUTPUT.PUT_LINE(to_char(student_rec.weight) || ' ' ||
nvl(to_char(student_rec.credit),'NULL'));
      IF      last_class = student_rec.class  --Repeat_delete 
          AND last_grade IN ('F', 'D')
          AND student_rec.grade IN ('A', 'B', 'C')
      THEN
         cum_weight :=   cum_weight
                       - last_weight
                       + student_rec.weight;
         cum_credits :=   cum_credits
                        - last_credit
                        + student_rec.credit;
      --DBMS_OUTPUT.PUT_LINE(to_char(student_rec.weight) ||
to_char(student_rec.credit));
      ELSE
         cum_weight :=   cum_weight
                       + student_rec.weight;
         cum_credits :=   cum_credits
                        + student_rec.credit;
      END IF;

      last_class := student_rec.class;
      last_weight := student_rec.weight;
      last_credit := student_rec.credit;
      last_grade := student_rec.grade;
      FETCH student_c INTO student_rec;
   END LOOP;

   CLOSE student_c;
   gpa := cum_weight / cum_credits;
   -- DBMS_OUTPUT.PUT_LINE('Cum Weight:' || to_char(cum_weight) || ' Cum
Credits:' || to_char(cum_credits));
   RETURN gpa;
END; -- Function CALC_AVG

Call it for every distinct student# using:

select calc_avg(student),a.student from 
(select distinct STUDENT# student from enrollment) a


-----Original Message-----
Sent: Monday, December 03, 2001 12:12 PM
To: Multiple recipients of list ORACLE-L


Hi, Could any one show me how to write the following
function?  Thank you very, very much!  

The GPA Function

1. A GPA is calculated in the following fashion:
Assume a student receives
an A on a 3-credit_hour course and a D on a
2-credit-hour course. His grade
is (4*3+1*2)/(3+2)=2.8.

2.Repeat_delete Policy: A student may repeat a course
as many times as he
wants. However, if the first grade he receives on this
course is a D or F,
then the second grade will automatically replaces the
first grade, and the
first enrollment will not go into his GPA calculation.
Under any other
circumstances, his grades will be considered as a
regular grade and be
taken into consideration for GPA.

create table Enrollment(
Student# NUMBER (7),
SNUM NUMBER (5),
Call# NUMBER (7),
Semester char (8),
GRADE char (3),
Credit number,
Withdraw_Date Date);

SNUM + Call# represents one class.

 Insert into Enrollment
values('58001','111','70070','Sp2000','F',3, null);
Insert into Enrollment
values('58001','111','70070','Fa2000','B',3, null);
Insert into Enrollment
values('58003','222','70070','Sp2000','A',2, null);
Insert into Enrollment
values('58004','333','80025','Fa2000','A',2, null);
Insert into Enrollment
values('58005','222','80025','Fa2000','C',3, null);




__________________________________________________
Do You Yahoo!?
Buy the perfect holiday gifts at Yahoo! Shopping.
http://shopping.yahoo.com
-- 
Please see the official ORACLE-L FAQ: http://www.orafaq.com
-- 
Author: Andrea Oracle
  INET: [EMAIL PROTECTED]

Fat City Network Services    -- (858) 538-5051  FAX: (858) 538-5051
San Diego, California        -- Public Internet access / Mailing Lists
--------------------------------------------------------------------
To REMOVE yourself from this mailing list, send an E-Mail message
to: [EMAIL PROTECTED] (note EXACT spelling of 'ListGuru') and in
the message BODY, include a line containing: UNSUB ORACLE-L
(or the name of mailing list you want to be removed from).  You may
also send the HELP command for other information (like subscribing).
-- 
Please see the official ORACLE-L FAQ: http://www.orafaq.com
-- 
Author: Scott Crabtree
  INET: [EMAIL PROTECTED]

Fat City Network Services    -- (858) 538-5051  FAX: (858) 538-5051
San Diego, California        -- Public Internet access / Mailing Lists
--------------------------------------------------------------------
To REMOVE yourself from this mailing list, send an E-Mail message
to: [EMAIL PROTECTED] (note EXACT spelling of 'ListGuru') and in
the message BODY, include a line containing: UNSUB ORACLE-L
(or the name of mailing list you want to be removed from).  You may
also send the HELP command for other information (like subscribing).

Reply via email to