如何在此透视查询中获得行总和?

时间:2013-12-03 20:02:22

标签: sql sql-server tsql pivot

我有以下查询,然后将其中的数据从表中重新生成为某些存储过程使用的格式,这些存储过程动态获取相同的数据:

2010-2011   4   3805    953 0   0   0   NULL    NULL    NULL    NULL    0.08399832  79.9034 20.0126 0   0   0   NULL    NULL    NULL    NULL
2011-2012   81  3867    971 0   0   0   NULL    NULL    NULL    NULL    1.646676    78.61354    19.73978    0   0   0   NULL    NULL    NULL    NULL
2012-2013   4   3706    918 0   0   0   NULL    NULL    NULL    NULL    0.08643042  80.07779    19.83578    0   0   0   NULL    NULL    NULL    NULL

这是SQL:

            DECLARE @r TABLE (SchoolYearDescription nvarchar(20), StackPosition int, Band_Count real, Band_Percent real)

        INSERT INTO @r
        SELECT y.SchoolYearDescription, rb.StackPosition, ISNULL(SUM(b.Band_Count), 0) AS 'Count', ISNULL(SUM(b.Band_Percent * b.Band_Count)
                    / NULLIF(SUM(b.Band_Count), 0), 0) AS 'Percent'
        FROM RosterSummaryData_Subject_Local g
            INNER JOIN RosterSummaryData_Subject_Local_Bands b ON g.pkSummarySubjectLocalID = b.fkSummarySubjectLocalID
            INNER JOIN PerformanceLevelReportBands rb ON b.fkBandID = rb.pkPerformanceLevelReportBandID
            RIGHT JOIN MM_SchoolYears y ON g.fkSchoolYearID = y.pkSchoolYearID
            INNER JOIN itot(@strYearIds, N',') tblYearIds ON y.pkSchoolYearId = tblYearIds.number
            INNER JOIN itot(@strDemoCodeIds, N',') tblDemoCodes ON g.fkDemoCommonCodeID = tblDemoCodes.number
            WHERE g.fkRosterSetID = @intRosterSetId
                AND g.fkTestInstanceID = CASE WHEN @intTestInstId = 0 THEN g.fkTestInstanceID ELSE @intTestInstId END
                AND g.fkTestTypeID = @intTestTypeId
                AND g.fkSchoolID = @intSchoolId
                AND g.fkGradeID = @intGradeId
                AND g.fkDepartmentID = @intDeptId
                AND g.fkCourseID = @intCourseId
                AND g.fkPeriodID = @intPeriodId
                AND g.fkTest_SubjectID IN (SELECT id FROM @tempSubs)
                AND rb.fkPerformanceLevelReportID = @intPerfLevelReportId
       GROUP BY y.SchoolYearDescription, rb.StackPosition
       ORDER BY y.SchoolYearDescription, rb.StackPosition

       SELECT * FROM
       (SELECT SchoolYearDescription ,
          CASE 
            WHEN col = 'Band_Count' THEN 'Count_'
            WHEN col = 'Band_Percent' THEN 'Percent_'
            END + CAST(StackPosition as varchar(1)) colName, value
       FROM @r
        UNPIVOT
        (
         value
         FOR col IN ([Band_Count], [Band_Percent])
        ) unpiv) src
        PIVOT
        (MAX(value)
        FOR colName IN (Count_0,Count_1,Count_2,Count_3,Count_4,Count_5,Count_6,Count_7,Count_8,Count_9,
        Percent_0,Percent_1,Percent_2,Percent_3,Percent_4,Percent_5,Percent_6,Percent_7,Percent_8,Percent_9) ) piv

   END

如何添加Count_Include列,该列是每行的Count_0到Count_9的总和?

感谢您的帮助,这超出了我的专业范围。

1 个答案:

答案 0 :(得分:1)

不是很令人兴奋,但你可以添加它们:

SELECT *
      ,ISNULL(Count_0,0) + ISNULL(Count_1,0) + ISNULL(Count_2,0) + ISNULL(Count_3,0) etc.. AS Count_Include
FROM
       (SELECT SchoolYearDescription ,
          CASE 
            WHEN col = 'Band_Count' THEN 'Count_'
            WHEN col = 'Band_Percent' THEN 'Percent_'
            END + CAST(StackPosition as varchar(1)) colName, value
       FROM @r
        UNPIVOT
        (
         value
         FOR col IN ([Band_Count], [Band_Percent])
        ) unpiv) src
        PIVOT
        (MAX(value)
        FOR colName IN (Count_0,Count_1,Count_2,Count_3,Count_4,Count_5,Count_6,Count_7,Count_8,Count_9,
        Percent_0,Percent_1,Percent_2,Percent_3,Percent_4,Percent_5,Percent_6,Percent_7,Percent_8,Percent_9) ) piv