Monday, September 29, 2008

Oracle PL/SQL | Question

/* To grnerate report for student Marks
Input Data:
STUDENT_ID MARKS SUBJECT
---------- ---------- --------------------
1 20 Maths
1 70 English
1 50 Science
1 45 Biology
2 50 Maths
2 30 English
2 50 Science
2 45 Biology
3 90 Maths
3 70 English
3 50 Science
3 35 Biology
Output Data:
1 145, 45.66, F ( if one of the marks < 40 then F(fail) else P (pass))
2 180,67.88 , P */

set line 3000;
set pagesize 100;
set head on;
with table01 as(
select 1 STUDENT_ID, 20 MARKS, 'Maths' SUBJECT from dual
union all
select 1,70, 'English' from dual
union all
select 1,50, 'Science' from dual
union all
select 1,45, 'Biology' from dual
union all
select 2, 50, 'Maths' from dual
union all
select 2, 30, 'English' from dual
union all
select 2, 50, 'Science' from dual
union all
select 2, 45, 'Biology' from dual
union all
select 3, 90, 'Maths' from dual
union all
select 3,70, 'English' from dual
union all
select 3, 50, 'Science' from dual
union all
select 3,35, 'Biology' from dual
)
select student_id, Total, Average,
case
when Minimum < 40
then
'Fail'
else
'Pass'
end Result
from (select distinct student_id,
min(marks) over (partition by student_id order by student_id ) Minimum,
sum(marks) over (partition by student_id order by student_id ) Total,
avg(marks) over (partition by student_id order by student_id ) Average
from table01
);

No comments: