我有一个包含标记条目的表。在这里我要准备一个特定学生的进度表。以便根据测试输入表格中的分数。
- 测试 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
有人有建议吗?