Hey… I have this database and I need to create a function that returns the percentage of students that passed a subject but I don´t know what I did wrong… Could you help me please??? Here is my diagram
and here is my function…
create function fnPassedStudents(@Semester varchar(7),@CodSubjects varchar(5))
returns @Passed
TABLE (
Semester varchar(7),
Cod_Subjects varchar(5),
Name_Subjects varchar(80),
Nro_Students int,
Nro_Passed float,
Nro_Failed float,
PercentagePassed varchar(4))
as
begin
declare @NroPassed float
select @NroPassed = count(M.Cod_Student)
from Matricula M inner join Subjects A on M.Cod_Subjects = A.Cod_Subjects
Where M.Semester=@Semester and M.Cod_Subjects = @CodSubjects and M.Grade>=10
insert into @Passed
select M.Semester, A.Cod_Subjects, A.Name_Subjects, COUNT(M.Cod_Student) as Total,
@NroPassed as Passed, (COUNT(M.Cod_Student) - @NroPassed) as Failed,
((@NroPassed * 100)/(count(M.Cod_Student))) as PercentagePassed
from Matricula M inner join Subjects A on M.Cod_Subjects = A.Cod_Subjects
Group by M.Semester, A.Cod_Subjects, A.Name_Subjects
return
end
go
I´m not sure if this information is enough… but please be patient and I´ll add anything you need… Thanks!!!
Ingredients
count(distinct M.cod_student)– number of students taking a semester/subjectcase when M.Grade>=10 then 1 end– produces 1 when passed, null otherwise. NULLs are not COUNTedcount(*) - count(case..)– the complement of passed = failed