Help needed on SQL Query..
515228Apr 27 2007 — edited Apr 27 2007Hi all,
how are u.....i have a problem in SQl query..actually i want to solve it myself since last one month but i cant so i am posting here so that all oracle GURU will help me...thanks in advance for reading my problem....
I am working with a university'examination department.....we have to calculate the CGPA and Store it to database....however currently we just calculate it on reports only on run time...but know we want to save it on database.....
on run time i give the folowing parametes,
Semmeter that is 1 ,2 ,3
ID, Sujest the city that is 1 for isb,2 for khi and etc..
Year, just digit like 2004,2005,
Degree, abbrievated as CODE in query....like BBA,MBA,BCS etc
Now i sujest that first we Got the registration nos for the student and add it to form tabular block...that is succesfully done by this Code...
<code>
declare
cursor C1 is
SELECT distinct(exam.REGISTRATION) registration,exam.enrollment
FROM EXAM, EXAMINATION, PROGRAM_REG
WHERE exam.examination_id=examination.examination_id and
program_reg.registration=exam.registration and
examination.id = program_reg.ID and
EXAMINATION.ID = :ID
and examination.code = program_reg.code and
EXAMINATION.CODE = :CODE
AND PROGRAM_REG.SEMESTER = :Semester
AND PROGRAM_REG.year = :year
PROGRAM_REG.YEAR,
order by exam.registration ;
begin
first_record;
for I in C1 loop
:registration:=I.registration;
:enrollment := I.enrollment;
next_record;
end loop;
</code>
Now i want to calculate CGPA from like this
this is tabular block in Oracle forms like this
Registration Enrolment CGPA
12213 12323
12312 123123
12323 123432
12323 2132313
The registration and and enrollment is populat by cursor and loop with the obove code.....
I had write a code which Calculate CGPA but it only display it only in first ITem not in the next and othere like...
Registration Enrolment CGPA
23123 23123 (it Write only here)once
123213 213213 empty
123213 213123 --
I am Using this Code.....I know there is problem in the Loop but i cant solve it
here is code for CGPA
<CODE>
declare
v_credits number(10);
v_product1 number(10);
v_product2 number(10);
v_CGPA number(10);
V_TOTALPROGRAMCREDITS NUMBER(10);
v_creditscompleted NUMBER(10);
V_DUR VARCHAR2(20);
begin
CGPA CALCULATION-------
-------Credits
select sum(CREDITS) INTO V_CREDITS from BUEXAMINATION.COURSES
where ID=:ID AND CODE=:CODE AND coursecode in
(select distinct(coursecode) from BUEXAMINATION.EXAMINATION E1,BUEXAMINATION.EXAM E
where (YEAR||SEMESTER) <= (:YEAR||:SEMESTER)
AND ID =:ID
AND (PROGRAM !=0 OR PROGRAM IS NULL)
AND E1.examination_id = E.examination_id
AND E.GRADE != 'W'
AND ((E.REMARKS != 'RELEGATED' OR E.REMARKS IS NULL))
-- AND ((E.REMARKS != 'RELEGATED' OR E.REMARKS IS NULL)OR ((E1.YEAR||E1.SEMESTER)=(P_YEAR||E1.SEMESTER)))
AND E.GRADE IS NOT NULL
AND E.ENROLLMENT=:ENROLLMENT
AND E.registration=:REGISTRATION );
---------CP_Product1
SELECT NVL(sum(MAX(PRODUCT)),0) INTO V_PRODUCT1
FROM BUEXAMINATION.EXAM E, BUEXAMINATION.EXAMINATION E1,BUEXAMINATION.COURSES C
WHERE E.EXAMINATION_ID = E1.EXAMINATION_ID
AND REGISTRATION=:REGISTRATION
AND ENROLLMENT=:ENROLLMENT
AND (YEAR||SEMESTER) <=(:YEAR||:SEMESTER)
AND C.COURSECODE = E1.COURSECODE
AND C.ID=:ID
AND (REMARKS != 'RELEGATED' OR REMARKS IS NULL)
--AND ((E.REMARKS != 'RELEGATED' OR E.REMARKS IS NULL)OR ((E1.YEAR||E1.SEMESTER)=(P_YEAR||E1.SEMESTER)))
AND (C.PROGRAM !=0 OR C.PROGRAM IS NULL)AND
E1.COURSECODE IN
(SELECT COURSECODE FROM BUEXAMINATION.EXAMINATION E1,BUEXAMINATION.EXAM E
WHERE (YEAR||SEMESTER) <=(:YEAR||:SEMESTER)
AND E1.EXAMINATION_ID = E.EXAMINATION_ID
AND REGISTRATION=:REGISTRATION
AND ENROLLMENT=:ENROLLMENT
GROUP BY (COURSECODE)
HAVING COUNT (COURSECODE)>1)
GROUP BY (E1.COURSECODE);
-------------prodeut2-----
SELECT NVL(sum(MAX(PRODUCT)),0) INTO V_PRODUCT2
FROM BUEXAMINATION.EXAM E, BUEXAMINATION.EXAMINATION E1,BUEXAMINATION.COURSES C
WHERE E.EXAMINATION_ID = E1.EXAMINATION_ID
AND REGISTRATION=:REGISTRATION
AND ENROLLMENT=:ENROLLMENT
AND (YEAR||SEMESTER) <=(:YEAR||:SEMESTER)
AND C.COURSECODE = E1.COURSECODE
AND C.ID=:ID
AND (REMARKS != 'RELEGATED' OR REMARKS IS NULL)
--AND ((E.REMARKS != 'RELEGATED' OR E.REMARKS IS NULL)OR ((E1.YEAR||E1.SEMESTER)=(P_YEAR||E1.SEMESTER)))
AND (C.PROGRAM !=0 OR C.PROGRAM IS NULL)AND
E1.COURSECODE NOT IN
(SELECT COURSECODE FROM BUEXAMINATION.EXAMINATION E1,BUEXAMINATION.EXAM E
WHERE (YEAR||SEMESTER) <=(:YEAR||:SEMESTER)
AND E1.EXAMINATION_ID = E.EXAMINATION_ID
AND REGISTRATION=:REGISTRATION
AND ENROLLMENT=:ENROLLMENT
GROUP BY (COURSECODE)
HAVING COUNT (COURSECODE)>1)
GROUP BY (E1.COURSECODE);
v_cgpa := Round((V_PRODUCT1+V_PRODUCT2) /V_CREDITS, 2);
CREIDITS COMPLETED--
SELECT SUM(CREDITS) INTO V_CREDITSCOMPLETED FROM BUEXAMINATION.COURSES
WHERE ID=:ID
AND CODE =:CODE
-- AND PROGRAM != 0
AND COURSECODE IN(
SELECT DISTINCT(COURSECODE) FROM BUEXAMINATION.EXAMINATION E,BUEXAMINATION.EXAM E1
WHERE E.EXAMINATION_ID=E1.EXAMINATION_ID
AND ID =:ID
-- AND E.CODE = :P_CODE
AND REGISTRATION = :REGISTRATION
AND ENROLLMENT = :ENROLLMENT
AND GRADE != 'W'
AND GRADE != 'F'
AND (REMARKS != 'RELEGATED' OR REMARKS IS NULL)
HAVING PRODUCT = MAX(PRODUCT)
GROUP BY PRODUCT,COURSECODE
);
---DURATION OF DEGREE
select program into V_DUR from buexamination.program_reg
where registration=:registration AND ENROLLMENT=:ENROLLMENT
AND ID = :ID
AND YEAR = :YEAR
AND CODE = :CODE;
--TOTAL CREIDITS OF DEGREE
SELECT CREDITS INTO V_TOTALPROGRAMCREDITS FROM BUEXAMINATION.PROGRAMS_DETAIL WHERE CODE=CODE AND DURATION=V_DUR;
--UPDATE TABLES......
IF v_creditscompleted >= V_TOTALPROGRAMCREDITS
THEN UPDATE STUDENT_TEST SET FINAL_CGPA = V_CGPA WHERE REGISTRATION = :REGISTRATION;
commit;
ELSE
UPDATE STUDENT_TEST SET CGPA = V_CGPA WHERE REGISTRATION = :REGISTRATION;
commit;
END IF;
MESSAGE('THE RECORD UPDATE SUCCESSFULLY');
end;
when i removed the update command it only show one CGPA but with Update command it erros....WHEN BUTTON PRESSED TRIGER RAISED UNHANDELED EXCEPTION.......
Any help Regarding this is highly appreciated........
Thanks in advance to all of you Oracle masters..
Shayan
coolmaxi82@hotmail.com