我必须显示如下结果:
Sr.No. | Subject Code | Name of Subject | Marks obtained Lowest | Marks obtained Highest | No. of students Appeared | No of Students Passed | Percent of Pass | Percent of above 60% students|
超过4个科目
ALTER proc [dbo].[Summery_TestMarks]
(
@courseId int,@SchemeID int, @AcadYear nvarchar(15), @Semest nvarchar(15)
)
AS
select SM.Subj_Code, SM.Subj_Name, Min(TM1.Test1_Marks) as Lowest, MAX(TM1.Test1_Marks) as Highest, count(TM1.Stud_ID) as appeared,
(select Count(TM1.STUD_ID) from Test1_Marks TM1
inner join SUBJECT_MASTER SM on TM1.Subj_ID = SM.Subject_ID
where TM1.Test1_Marks> 9
AND TM1.Course_ID = @courseId AND TM1.Scheme_ID = @SchemeID AND TM1.Semester = @Semest AND TM1.Acad_Year = @AcadYear
)as Passed,
(select Count(TM1.STUD_ID) from Test1_Marks TM1
inner join SUBJECT_MASTER SM on TM1.Subj_ID = SM.Subject_ID
where TM1.Test1_Marks> 9
AND TM1.Course_ID = @courseId AND TM1.Scheme_ID = @SchemeID AND TM1.Semester = @Semest AND TM1.Acad_Year = @AcadYear )*100/ (count(TM1.Stud_ID))
as percentage,
((select count(TM1.STUD_ID) as above60 from Test1_Marks TM1 where (TM1.Test1_Marks*100/25) > 60 )*100/
(select count(TM1.STUD_ID) from Test1_Marks TM1)) as above60 from Test1_Marks TM1
inner join SUBJECT_MASTER SM on TM1.Subj_ID = SM.Subject_ID
where TM1.Course_ID = @courseId AND TM1.Scheme_ID = @SchemeID AND TM1.Semester = @Semest AND TM1.Acad_Year = @AcadYear
group by SM.Subj_Name, SM.Subj_Code
我遇到了这个错误。我怎么解决这个问题? 我已经尝试了很多,但它给出了问题。
答案 0 :(得分:1)
您可以将CASE ... END
置于SUM()
内,从而大大简化此操作。这是您的查询的缩写版本,仅用于演示基本概念:
SELECT tm.CourseID, MIN(Test1_Marks) [LowestScore], MAX(Test1_Marks) [HighestScore], COUNT(Stud_ID) [NumberOfStudents]
, SUM(CASE WHEN Test1_Marks > 9 THEN 1 ELSE 0 END) [NumberWhoPassed]
, SUM(CASE WHEN Test1_Marks > 9 THEN 1 ELSE 0 END) / COUNT(Stud_ID) [PercentWhoPassed]
, SUM(CASE WHEN TM1.Test1_Marks*100/25) > 60 THEN 1 ELSE 0 END) / COUNT(Stud_ID) [PercentAbove60]
FROM Test_Marks tm
GROUP BY tm.CourseID
答案 1 :(得分:1)
试试这个:
;WITH CTE AS
(
SELECT SM.Subj_Code,
SM.Subj_Name,
MIN(TM1.Test1_Marks) AS Lowest,
MAX(TM1.Test1_Marks) AS Highest,
COUNT(TM1.Stud_ID) AS Appeared,
COUNT(CASE WHEN TM1.Test1_Marks > 9 THEN TM1.STUD_ID END) AS Passed,
COUNT(CASE WHEN TM1.Test1_Marks*100/25) > 60 THEN TM1.STUD_ID END) * 100 /
COUNT(TM1.Stud_ID) AS Above60
FROM Test1_Marks TM1
INNER JOIN SUBJECT_MASTER SM
ON TM1.Subj_ID = SM.Subject_ID
WHERE TM1.Course_ID = @courseId
AND TM1.Scheme_ID = @SchemeID
AND TM1.Semester = @Semest
AND TM1.Acad_Year = @AcadYear
GROUP BY SM.Subj_Name, SM.Subj_Code
)
SELECT Subj_Code,
Subj_Name,
Lowest,
Highest,
Appeared,
Passed,
Passed*100.0/Appeared AS Percentage
Above60
FROM CTE
答案 2 :(得分:0)
纯粹作为这种情况下的故障排除策略,如果你没有,你可以做的一件事就是取出所有这些小子选择并对你的数据库单独运行它们以确保它们给出有意义的结果而不是错误。
答案 3 :(得分:0)
我得到了@Joel Coehoorn帮助的结果 -
select TM.Subj_ID, SM.Subj_Name ,MIN(TM.Test1_Marks) [LowestScore], MAX(TM.Test1_Marks)[HighestScore], COUNT(TM.STUD_ID) [NumberOfStudents],
SUM(CASE WHEN TM.Test1_Marks > 9 THEN 1 ELSE 0 END)[NumberWhoPassed],
SUM(CASE WHEN TM.Test1_Marks > 9 THEN 1 ELSE 0 END)*100 / count(TM.STUD_ID) [PercentWhoPassed],
SUM(CASE WHEN (TM.Test1_Marks * 100 / 25)>60 THEN 1 ELSE 0 END)*100/ count(TM.STUD_ID) [PercentAbove60]
from Test1_Marks TM
join SUBJECT_MASTER SM on TM.Subj_ID = SM.Subject_ID
WHERE TM.Course_ID = 1 AND TM.Scheme_ID = 2 AND TM.Semester = '3rd Semester' AND TM.Acad_Year = '2013-2014'
GROUP BY TM.Subj_ID, SM.Subj_Name
感谢所有参与者表现出兴趣... @Adam Miller,@ Lamak,@ Dan Bracuk,@ Joel Coehoorn