0

我在 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 语法

谢谢

4

2 回答 2

3

尝试这个。如果您的查询仍然运行缓慢,则需要检查表上是否有索引。如果您的表很大并且没有索引,它仍然可能很慢。

select
    StM.StudentName,
    SbjM.SubjectName,
    count(case when AtM.Attendance = 'P' then 1 else null end) as Total(P),
    count(case when AtM.Attendance = 'P1' then 1 else null end) as Total(P1),
    count(case when AtM.Attendance = 'P2' then 1 else null end) as Total(P2)
from StudentMaster  as StM
    inner join AttendanceMaster as AtM on AtM.StudentId = StM.StudentId
    inner join SubjectMaster as SbjM on SbjM.SubjectId = AtM.SubjectId
group by
    StM.StudentName,
    SbjM.SubjectName

如果列Attendance除了 P、P1 和 P2 之外没有任何值,则要计算总数,您只需添加count(*) as Total. 如果除了 P、P1 和 P2 之外还有其他值,有两种方法。首先 - 您可以在 where 子句的 ('P', 'P1', 'P2') 中添加 AtM.Attendance:

select
    StM.StudentName,
    SbjM.SubjectName,
    count(case when AtM.Attendance = 'P' then 1 else null end) as Total(P),
    count(case when AtM.Attendance = 'P1' then 1 else null end) as Total(P1),
    count(case when AtM.Attendance = 'P2' then 1 else null end) as Total(P2),
    count(*) as Total
from StudentMaster  as StM
    inner join AttendanceMaster as AtM on AtM.StudentId = StM.StudentId
    inner join SubjectMaster as SbjM on SbjM.SubjectId = AtM.SubjectId
where AtM.Attendance in ('P', 'P1', 'P2')
group by
    StM.StudentName,
    SbjM.SubjectName

或者你可以这样写

select
    StM.StudentName,
    SbjM.SubjectName,
    count(case when AtM.Attendance = 'P' then 1 else null end) as Total(P),
    count(case when AtM.Attendance = 'P1' then 1 else null end) as Total(P1),
    count(case when AtM.Attendance = 'P2' then 1 else null end) as Total(P2),
    count(case when AtM.Attendance in ('P', 'P1', 'P2') then 1 else null) as Total
from StudentMaster  as StM
    inner join AttendanceMaster as AtM on AtM.StudentId = StM.StudentId
    inner join SubjectMaster as SbjM on SbjM.SubjectId = AtM.SubjectId
group by
    StM.StudentName,
    SbjM.SubjectName
于 2012-10-26T06:17:23.863 回答
0
select MAX(sm.StudentName) as StudentName,
       MAX(subm.SubjectName) as SubjectName,
       SUM(CASE WHEN am.Attendance='P' then 1 else 0 end) as TotalP,
       SUM(CASE WHEN am.Attendance='P1' then 1 else 0 end) as TotalP1,
       SUM(CASE WHEN am.Attendance='P2' then 1 else 0 end) as TotalP2

from AttendanceMaster am inner join StudentMaster sm
on am.StudentId=sm.StudentId
inner join SubjectMaster subm on am.SubjectId=subm.SubjectId

group by am.StudentId,
         am.SubjectId
于 2012-10-26T06:25:11.937 回答