我有一個包含Mark條目的表。在這裏,我想準備一個特定的學生的進度表。因此,根據測試,在表中輸入的標誌。SQL Server查詢中的問題
- 試驗1包含的主題的所有標記即,英語,植物學等
- 試驗2也包含主題
對於我使用union all
查詢的標記基於testid
select distinct
W.SubjectName, W.StudentId, W.Test1, W.Test2
from
(select distinct
SB.SubjectName, ME.StudentId, ME.Mark as Test1, 0 as Test2
from
MarkEntry ME
inner join
Subject SB on SB.SubjectId = ME.SubjectId
where
ME.TestId = 1
and ME.GradeId = 5
and ME.SectionId = 9
and ME.TermId = 1
and ME.LevelId = 1
and ME.StreamId = 2
and ME.AcYear = 14
group by
ME.Mark, ME.StudentId, SB.SubjectName
union all
select distinct
SB.SubjectName, ME.StudentId, 0 as Test1, ME.Mark as Test2
from
MarkEntry ME
inner join
Subject_DT SB on SB.SubjectId = ME.SubjectId
where
ME.TestId = 2
and ME.GradeId = 5
and ME.SectionId = 9
and ME.TermId = 1
and ME.LevelId = 1
and ME.StreamId = 2
and ME.AcYear = 14
group by
ME.Mark, ME.StudentId, SB.SubjectName) W
where
W.StudentId = 1052
group by
W.StudentId, W.Test1, W.Test2, W.SubjectName
我的結果是這樣的:
SubjectName StudentId Test1 Test2
-------------------------------------------------
English 1052 0.0 23.0
Botany 1052 0.0 32.0
Zoology 1052 0.0 32.0
Botany 1052 10.0 0.0
English 1052 10.0 0.0
Zoology 1052 20.0 0.0
但我需要像這樣
SubjectName StudentId Test1 Test2
---------------------------------------------
English 1052 10.0 23.0
Botany 1052 10.0 32.0
Zoology 1052 20.0 32.0
任何人有什麼建議?
+1 Yep,但它需要'MarkEntry(TestId,StudentId)'或'MarkEntry(StudentId,TestId)'上的唯一索引/約束。否則,一些學生可能有兩個或更多行用於同一個'TestId'。 –