0

SQLServerデータベースに次のテーブルがあります。

(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以外の値がある場合は、2つの方法があります。まず、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 に答える