开发者

I don´t know what´s wrong with this function

开发者 https://www.devze.com 2023-02-16 19:27 出处:网络
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 d

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

I don´t know what´s wrong with this function

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开发者_StackOverflow中文版 @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!!!


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
insert into @Passed 
select
    M.Semester,
    A.Cod_Subjects,
    A.Name_Subjects,
    count(distinct M.cod_student),
    count(case when M.Grade>=10 then 1 end),
    count(*) - count(case when M.Grade>=10 then 1 end),
    count(case when M.Grade>=10 then 1 end) / count(distinct M.cod_student) * 100
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

Ingredients

  • CASE http://msdn.microsoft.com/en-us/library/ms181765.aspx
  • DISTINCT http://msdn.microsoft.com/en-us/library/ms176104.aspx

count(distinct M.cod_student) - number of students taking a semester/subject case when M.Grade>=10 then 1 end - produces 1 when passed, null otherwise. NULLs are not COUNTed count(*) - count(case..) - the complement of passed = failed

0

精彩评论

暂无评论...
验证码 换一张
取 消