我在 SQL Server 数据库中有以下表格:
(1) StudentMaster (StudentId, StudentName)
(2) SubjectMaster (SubjectId, SubjectName)
(3) AttendanceMaster (AttendanceId,StudentId,SubjectId,Attendance,Date)
Data in AttendanceMaster can be in Following format :
AttendanceMaster :
AttendanceId StudentId SubjectId Attendance Date
3001 33 1 P 1/1/2011
3001 57 2 P1 1/2/2011
3001 33 1 P 1/3/2011
3001 57 2 P2 1/4/2011
3001 33 1 P1 1/5/2011
我想获得以下格式的 SubjectWise 个人出勤详细信息:
StudentName SubjectName Total(P) Total(P1) Total(P2)
Ghanshyam Maths 90 10 5
John Maths 85 15 5
Ghanshyam Science 70 20 15
John Science 80 30 5
我尝试了以下查询:
select StudentName, SubjectName,
(select count(*) from AttendanceMaster innerAM where innerAM.StudentId = StdM.StudentId and innerAM.SubjectId=SubM.SubjectId and innerAM.Attendance = 'P') as Total(P),
(select count(*) from AttendanceMaster innerAM where innerAM.StudentId = StdM.StudentId and innerAM.SubjectId=SubM.SubjectId and innerAM.Attendance = 'P1') as Total(P1),
(select count(*) from AttendanceMaster innerAM where innerAM.StudentId = StdM.StudentId and innerAM.SubjectId=SubM.SubjectId and innerAM.Attendance = 'P1') as Total(P2)
from AttendanceMaster AM inner join StudentMaster StdM on AM.StudentId = StdM.StudentId
inner join SubjectMaster SubM on AM.SubjectId = SubM.SubjectId
我得到了结果,但是执行时间太长了..(大约 5 到 6 分钟)
so what can i do to decrease execution time...
并且编写查询以获取 Total(P),Toal(P1),Total(P2) 是否正确?请指定其他 SQL 语法
谢谢