具有聚合功能的SQL中的Dynamic Pivot

时间:2018-11-16 08:14:12

标签: sql-server

   CREATE TABLE #Te   (Id int Identity(1,1) ,SubjectListId int,SubjectName nvarchar(max), ExaminationSubjectId int, 
                                       ExaminationId nvarchar(max) ,
                                       Marks int, TotalMark int , Remark nvarchar(max),ExaminationName nvarchar(max),Grade nvarchar(max),MainExamId nvarchar(max),TotalForSubject Int, MaxMarkTotal int, TotalGrade  nvarchar(max) ,MainExaminationName nvarchar(max) ,MainExamSubjectTotal int )


                                   INSERT INTO #TE(SubjectListId,SubjectName,ExaminationSubjectId,ExaminationId,Marks,TotalMark,Remark,ExaminationName,Grade,MainExamId,TotalForSubject,MaxMarkTotal,TotalGrade,MainExaminationName,MainExamSubjectTotal)
                                   VALUES(1,'DataBase Management',63,48,12,20,null,'CA2','B2',1,24,140,NULL,'MCA SEM 1 ',24)

                                   INSERT INTO #TE(SubjectListId,SubjectName,ExaminationSubjectId,ExaminationId,Marks,TotalMark,Remark,ExaminationName,Grade,MainExamId,TotalForSubject,MaxMarkTotal,TotalGrade,MainExaminationName,MainExamSubjectTotal)
                                   VALUES(1,'DataBase Management',81,52,NULL,100,null,'CA Main Exam',NULL,3,24,140,NULL,'Board Exam ',0)

                                   INSERT INTO #TE(SubjectListId,SubjectName,ExaminationSubjectId,ExaminationId,Marks,TotalMark,Remark,ExaminationName,Grade,MainExamId,TotalForSubject,MaxMarkTotal,TotalGrade,MainExaminationName,MainExamSubjectTotal)
                                   VALUES(1,'DataBase Management',60,47,12,20,null,'CA1','B2',1,24,140,NULL,'MCA SEM 1 ',24)

                                   INSERT INTO #TE(SubjectListId,SubjectName,ExaminationSubjectId,ExaminationId,Marks,TotalMark,Remark,ExaminationName,Grade,MainExamId,TotalForSubject,MaxMarkTotal,TotalGrade,MainExaminationName,MainExamSubjectTotal)
                                   VALUES(2,'DataMining',82,52,NULL,100,null,'CA Main Exam',NULL,3,12,120,NULL,'Board Exam ',0)

                                   INSERT INTO #TE(SubjectListId,SubjectName,ExaminationSubjectId,ExaminationId,Marks,TotalMark,Remark,ExaminationName,Grade,MainExamId,TotalForSubject,MaxMarkTotal,TotalGrade,MainExaminationName,MainExamSubjectTotal)
                                   VALUES(2,'DataMining',64,48,12,20,null,'CA2','B2',1,12,120,NULL,'MCA SEM 1 ',12)

                                   INSERT INTO #TE(SubjectListId,SubjectName,ExaminationSubjectId,ExaminationId,Marks,TotalMark,Remark,ExaminationName,Grade,MainExamId,TotalForSubject,MaxMarkTotal,TotalGrade,MainExaminationName,MainExamSubjectTotal)
                                   VALUES(3,'Operating System',83,52,NULL,100,null,'CA Main Exam','NULL',3,NULL,100,NULL,'Board Exam ',0)

                                   INSERT INTO #TE(SubjectListId,SubjectName,ExaminationSubjectId,ExaminationId,Marks,TotalMark,Remark,ExaminationName,Grade,MainExamId,TotalForSubject,MaxMarkTotal,TotalGrade,MainExaminationName,MainExamSubjectTotal)
                                   VALUES(7,'Java',61,47,14,20,NULL,'CA1','B1',1,23,140,NULL,'MCA SEM 1  ',23)

                                   INSERT INTO #TE(SubjectListId,SubjectName,ExaminationSubjectId,ExaminationId,Marks,TotalMark,Remark,ExaminationName,Grade,MainExamId,TotalForSubject,MaxMarkTotal,TotalGrade,MainExaminationName,MainExamSubjectTotal)
                                   VALUES(7,'Java',65,48,9,20,NULL,'CA2','C2',1,23,140,NULL,'MCA SEM 1  ',23)

                                   INSERT INTO #TE(SubjectListId,SubjectName,ExaminationSubjectId,ExaminationId,Marks,TotalMark,Remark,ExaminationName,Grade,MainExamId,TotalForSubject,MaxMarkTotal,TotalGrade,MainExaminationName,MainExamSubjectTotal)
                                   VALUES(7,'Java',84,52,NULL,100,NULL,'CA Main Exam',NULL,3,23,140,NULL,'Board Exam ',0)

                                   INSERT INTO #TE(SubjectListId,SubjectName,ExaminationSubjectId,ExaminationId,Marks,TotalMark,Remark,ExaminationName,Grade,MainExamId,TotalForSubject,MaxMarkTotal,TotalGrade,MainExaminationName,MainExamSubjectTotal)
                                   VALUES(8,'Java Lab',62,47,4,20,NULL,'CA1','D2',1,23,40,'C1','MCA SEM 1  ',23)

                                   INSERT INTO #TE(SubjectListId,SubjectName,ExaminationSubjectId,ExaminationId,Marks,TotalMark,Remark,ExaminationName,Grade,MainExamId,TotalForSubject,MaxMarkTotal,TotalGrade,MainExaminationName,MainExamSubjectTotal)
                                   VALUES(8,'Java Lab',66,48,19,20,NULL,'CA2','A1',1,23,40,'C1','MCA SEM 1  ',23)

                                    INSERT INTO #TE(SubjectListId,SubjectName,ExaminationSubjectId,ExaminationId,Marks,TotalMark,Remark,ExaminationName,Grade,MainExamId,TotalForSubject,MaxMarkTotal,TotalGrade,MainExaminationName,MainExamSubjectTotal)
                                   VALUES(42916,'System Analysis and design ',85,52,NULL,100,NULL,'CA Main Exam',NULL,3,NULL,100,NULL,'Board Exam  ',0)

                                    INSERT INTO #TE(SubjectListId,SubjectName,ExaminationSubjectId,ExaminationId,Marks,TotalMark,Remark,ExaminationName,Grade,MainExamId,TotalForSubject,MaxMarkTotal,TotalGrade,MainExaminationName,MainExamSubjectTotal)
                                   VALUES(42917,'Networking',86,52,NULL,100,NULL,'CA Main Exam',NULL,3,NULL,100,NULL,'Board Exam  ',0)


      CREATE TABLE #mytransactions 
                    ( Examname nvarchar(max), Grade nvarchar(max),SubjectName nvarchar(max),Mark int,TotalForSubject int,SubjectGrade nvarchar(max),MainExamSubjectTotal int,MainExamId int,MainExamName nvarchar(max) )

                     INSERT INTO #mytransactions (Examname, Grade, SubjectName,Mark,SubjectGrade,TotalForSubject,MainExamSubjectTotal,MainExamId,MainExamName) 
                     SELECT ExaminationName,Grade, SubjectName,Marks,TotalGrade,TotalForSubject ,MainExamSubjectTotal,MainExamId ,MainExaminationName FROM #Te

                     DECLARE    @cols AS NVARCHAR(MAX),
                                @colsName AS NVARCHAR(MAX),
                                @colsName2 AS NVARCHAR(MAX),
                                @query  AS NVARCHAR(MAX),
                                @cols2  AS NVARCHAR(MAX)

                            select @cols = STUFF((SELECT distinct ',' + QUOTENAME(Examname +'_'+c.col) 
                                          from #mytransactions
                                          cross apply 
                                          (
                                            select 'Grade' col
                                            union all
                                            select 'Mark'
                                          ) c
                                FOR XML PATH(''), TYPE
                                ).value('.', 'NVARCHAR(MAX)') 
                            ,1,1,'')


                                select @cols2=  STUFF((SELECT distinct ',' + QUOTENAME(MainExamName +'_'+c.col) 
                                                  from #mytransactions
                                                  cross apply 
                                                  (
                                                    select 'Total' col

                                                  ) c
                                        FOR XML PATH(''), TYPE
                                        ).value('.', 'NVARCHAR(MAX)') 
                                    ,1,1,'')


                                SET @cols = CONCAT(@cols,',',@cols2 );

                                    select @colsName 
                                 = STUFF((SELECT distinct ', ' + QUOTENAME(MainExamName +'_'+c.col) 
                                   +' as ['
                                   + MainExamName + case when c.col = 'Total' then '  Total]'   end
                                 from #mytransactions
                                 cross apply 
                                 (
                                    select 'Total' col

                                 ) c
                                FOR XML PATH(''), TYPE
                                ).value('.', 'NVARCHAR(MAX)') 
                            ,1,1,'')



                            select @colsName2 
                        = STUFF((SELECT distinct ', ' + QUOTENAME(Examname +'_'+c.col) 
                                   +' as ['
                                   + Examname + case when c.col = 'Grade' then '  Grade]' else ' Mark]' end
                                 from #mytransactions
                                 cross apply 
                                 (
                                    select 'Grade' col
                                    union all
                                    select 'Mark'
                                 ) c
                                FOR XML PATH(''), TYPE
                                ).value('.', 'NVARCHAR(MAX)') 
                            ,1,1,'')



                                     set @query 
                      =   'SELECT SubjectName , ' + @colsName2 + ' ,'+@colsName +'
                         from 
                         (
                          select 
                            SubjectName, 
                            Examname +''_''+col col,  MainExamName +''_''+col col2,  
                            value
                          from
                          (
                            select   

                              SubjectName,
                              Examname, 
                              MainExamName,
                              cast(Grade as nvarchar(max)) Grade,
                              cast(Mark as  nvarchar(max)) Mark,
                              cast(MainExamSubjectTotal as  nvarchar(max)) MainExamSubjectTotal 

                            from #mytransactions
                          ) src
                          unpivot
                          (
                            value
                            for col in (Grade, Mark)
                          ) unpiv
                         ) s
                         pivot 
                         (
                           max(value)
                           for col in (' + @cols + ')
                         ) p 
                          '
                          print (@query)
                                    execute(@query)

我正在尝试生成期望格式为Expected output的报告卡 但是得到的是obtained output。我的要求就像有一组考试名称作为TERM ONE EXAM一样,因此在TERM ONE EXAM下将有一个CA1考试CA2考试,部分考试,需要总计,类似TERM2,TERM3的等级,这些考试下有多个考试并添加了更好的理解 。提前感谢您的帮助。

0 个答案:

没有答案