2011-03-14 131 views
1

嘿...我有這個數據庫,我需要創建一個函數,返回通過一個主題的學生的百分比,但我不知道我做錯了什麼... 請問你能幫幫我嗎???這是我的圖Diagram,這裏是我的功能...我不知道這個功能有什麼問題

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 [email protected] 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'll添加您需要什麼...謝謝! !

+0

它在做什麼或不做什麼?它會崩潰嗎?給出錯誤?或者只是不做你的期望? – Jason 2011-03-14 02:02:55

回答

3
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 

成分

count(distinct M.cod_student) - 學生採取學期/受試者 case when M.Grade>=10 then 1 end的數 - 產生1傳遞時,否則返回null。 NULLs不計COUNTED count(*) - count(case..) - 通過的補碼=失敗

+0

它工作完美!!!!!!非常感謝你......我有很多東西要學習T_T – brisonela 2011-03-14 02:12:32