与SQL Server中的内部查询相关的问题

时间:2012-10-26 06:08:53

标签: sql sql-server

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

由于

2 个答案:

答案 0 :(得分: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之外还有其他值,有两种方法。首先 - 你可以添加 atM.Attendance in('P','P1','P2')in where子句:

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

答案 1 :(得分: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