2013-08-07 67 views
0

我有一個包含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 

任何人有什麼建議?

回答

0

您幾乎已經知道了,您只需修復分組並選擇測試結果的最大值即可。

我覺得可能是這樣做的更好的方法,但這應該工作:

SELECT DISTINCT 
     W.SubjectName , 
     W.StudentId , 
     MAX(W.Test1) , 
     MAX(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.SubjectName 
3

您還可以使用PIVOT這一點。樞軸可以很容易地僅僅通過添加更多的值到FOR TestId IN ([1], [2])條款

SELECT * 
FROM (
    SELECT S.SubjectName, ME.StudentId, ME.TestID, ME.Mark 
    FROM Subject S 
    INNER JOIN MarkEntry ME ON S.SubjectId = ME.SubjectId 
    /* add this for your query 
    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 
    */ 
) AS StudentMarks 
pivot (max(Mark) FOR TestId IN ([1], [2]) 
) AS pvt 
WHERE StudentId = 1 

PIVOT

SQLFiddle here

+0

+1 Yep,但它需要'MarkEntry(TestId,StudentId)'或'MarkEntry(StudentId,TestId)'上的唯一索引/約束。否則,一些學生可能有兩個或更多行用於同一個'TestId'。 –

1

我缺少的東西,或者你可以這樣做有加入添加更多的測試?

select SB.SubjectName, ME1.StudentId, ME1.Mark as Test1, ME2.Mark as Test2 
from MarkEntry ME1 
inner join MarkEntry ME2 on ME2.SubjectId = ME1.SubjectId 
         and ME2.StudentId = ME1.StudentId 
inner join Subject SB on SB.SubjectId = ME1.SubjectId 
where ME1.TestId = 1 
and ME1.GradeId = 5 
and ME1.SectionId = 9 
and ME1.TermId = 1 
and ME1.LevelId = 1 
and ME1.StreamId = 2 
and ME1.AcYear = 14 

and ME2.TestId = 2 
and ME2.GradeId = 5 
and ME2.SectionId = 9 
and ME2.TermId = 1 
and ME2.LevelId = 1 
and ME2.StreamId = 2 
and ME2.AcYear = 14 

and ME1.StudentId = 1052