2016-02-27 75 views
1

我一直在寻找关于我的问题几个小时的答案。SQL Server:枢轴多个聚合体

我现在的表:

StudentName Course Correct Wrong Blank Score 
------------------------------------------------- 
Student1 Math 38  2  0  95 
Student1 English 45  5  0  90 
... 
Student2 Math 38  2  0  95 
Student2 English 45  5  0  90 

我要的是:

   Math        English 
StudentName Correct Wrong Blank Score Correct Wrong Blank Score 
Student1  38  2  0  95  45  5  0  90 
Student2  38  2  0  95  45  5  0  90` 

...

SELECT  dbo.tbl_Students.StudentName, 
      dbo.tbl_CourseCategories.CourseCategory, 
      dbo.tbl_GeneralTestsScores.Correct, 
      dbo.tbl_GeneralTestsScores.Wrong, 
      dbo.tbl_GeneralTestsScores.NotAnswered, 
      dbo.tbl_GeneralTestsScores.Score 
FROM  
      dbo.tbl_AcademicTermsStudents 
INNER JOIN 
      dbo.tbl_Students ON dbo.tbl_AcademicTermsStudents.StudentID = dbo.tbl_Students.StudentID 
INNER JOIN 
      dbo.tbl_GeneralTestsScores 
INNER JOIN 
      dbo.tbl_CourseCategories 

ON   dbo.tbl_GeneralTestsScores.CourseCategoryID = dbo.tbl_CourseCategories.CourseCategoryID 

ON   dbo.tbl_AcademicTermsStudents.StudentID = dbo.tbl_GeneralTestsScores.StudentID 
Order By StudentName 

我搜索了很多网页的任何不能与解决方案告终。

谢谢。

编辑:我也愿意接受以下的解决方案......

StudentName Math_C Math_W Math_B Math_S English_C English_W English_B English_S 
Student1  38  2  0  95  45   5  0  90 
Student2  38  2  0  95  45   5  0  90` 
+0

你会对c#中的客户端解决方案感到满意吗? http://www.codeproject.com/Articles/796651/Client-Side-Multi-Column-Dynamic-Pivot –

+0

不要在'SQL'中尝试使用应用程序层。另外两行Header在'SQL'中是不可能的 –

+0

感谢您的评论。 – Meliksah

回答

0

您可以通过转动前加入每科/得分组合一个新的独特的柱,以“双支点”实现这一目标。

下面是一个静态示例,您可以轻松将其转换为动态数据透视表以满足更多课程的需求。您也可以将您的原始查询放入CTE中,根据需要插入临时表或内联 - 为了清晰起见,我使用了单个临时表。

希望这会有所帮助。

--Test Data 
SELECT * INTO #Students FROM (VALUES 
('Student1','Math', 38,  2,  0,  95), 
('Student1','English', 45,  5,  0,  90), 
('Student2','Math', 38,  2,  0,  95), 
('Student2','English', 45,  5,  0,  90) 
) A (StudentName, CourseName, Correct, Blank, Wrong, Score) 

--Pivoting 
SELECT StudentName 
     ,SUM(Math_Correct) Math_Correct 
     ,SUM(Math_Blank) Math_Blank 
     ,SUM(Math_Wrong) Math_Wrong 
     ,SUM(Math_Score) Math_Score 
     ,SUM(English_Correct) English_Correct 
     ,SUM(English_Blank) English_Blank 
     ,SUM(English_Wrong) English_Wrong 
     ,SUM(English_Score) English_Score 
FROM 
    (SELECT 
     S.StudentName 
     ,S.CourseName+'_Correct' CourseNameCorrrect 
     ,S.CourseName+'_Blank' CourseNameBlank 
     ,S.CourseName+'_Wrong' CourseNameWrong 
     ,S.CourseName+'_Score' CourseNameScore 
     ,S.Correct 
     ,S.Blank 
     ,S.Wrong 
     ,S.Score  
    FROM #Students S) S2 
    PIVOT(MAX(Correct) FOR CourseNameCorrrect IN ([Math_Correct], [English_Correct])) P1 
    PIVOT(MAX(Blank) FOR CourseNameBlank IN ([Math_Blank], [English_Blank])) P2 
    PIVOT(MAX(Wrong) FOR CourseNameWrong IN ([Math_Wrong], [English_Wrong])) P3 
    PIVOT(MAX(Score) FOR CourseNameScore IN ([Math_Score], [English_Score])) P4 
    GROUP BY StudentName 

StudentName Math_Correct Math_Blank Math_Wrong Math_Score English_Correct English_Blank English_Wrong English_Score 
----------- ------------ ----------- ----------- ----------- --------------- ------------- ------------- ------------- 
Student1 38   2   0   95   45    5    0    90 
Student2 38   2   0   95   45    5    0    90