真正简单的PIVOT示例MS SQL 2014

时间:2016-09-01 18:38:38

标签: sql-server-2014

我可以为数据透视表找到的所有示例都涉及聚合,并且看起来太复杂而无法实现我想要做的事情,这看起来应该非常简单:采用具有固定行数和存在的现有表列中的数据而不是行格式。

这是一些神话般的数据:

DECLARE @HSData TABLE (

      School            NVARCHAR(20)
    , Sophomores        INTEGER
    , Juniors           INTEGER
    , Seniors           INTEGER
    , Total             INTEGER
    )

INSERT INTO @HSData VALUES
      ('Armstrong HS',397,337,318,0)
    , ('Buchanan HS',383,325,308,0)
    , ('Carlsbad HS',338,321,285,0)

UPDATE @HSData
    SET Total = Sophomores + Juniors + Seniors

 SELECT * FROM @HSData

结果如下:

enter image description here

我想要的是这样的数据:

enter image description here

我可以通过使用以下代码手动将数据插入变量来实现此目的:

DECLARE 
          @AName            NVARCHAR(20)
        , @BName            NVARCHAR(20)
        , @CName            NVARCHAR(20)
        , @A10              INTEGER
        , @A11              INTEGER
        , @A12              INTEGER
        , @B10              INTEGER
        , @B11              INTEGER
        , @B12              INTEGER
        , @C10              INTEGER
        , @C11              INTEGER
        , @C12              INTEGER

SET @AName = (SELECT School FROM (SELECT School, ROW_NUMBER() OVER (ORDER BY School) RN FROM @HSData) SR WHERE RN = 1)
SET @BName = (SELECT School FROM (SELECT School, ROW_NUMBER() OVER (ORDER BY School) RN FROM @HSData) SR WHERE RN = 2)
SET @CName = (SELECT School FROM (SELECT School, ROW_NUMBER() OVER (ORDER BY School) RN FROM @HSData) SR WHERE RN = 3)
SET @A10  = (SELECT Sophomores FROM @HSData WHERE School = @AName)
SET @B10  = (SELECT Sophomores FROM @HSData WHERE School = @BName)
SET @C10  = (SELECT Sophomores FROM @HSData WHERE School = @CName)
SET @A11  = (SELECT Juniors    FROM @HSData WHERE School = @AName)
SET @B11  = (SELECT Juniors    FROM @HSData WHERE School = @BName)
SET @C11  = (SELECT Juniors    FROM @HSData WHERE School = @CName)
SET @A12  = (SELECT Seniors    FROM @HSData WHERE School = @AName)
SET @B12  = (SELECT Seniors    FROM @HSData WHERE School = @BName)
SET @C12  = (SELECT Seniors    FROM @HSData WHERE School = @CName)

    DECLARE @HSData2 TABLE (

          Class                 NVARCHAR(20)
        , "Armstrong HS"        INTEGER
        , "Buchanan HS"         INTEGER
        , "Carlsbad HS"         INTEGER
        )

INSERT INTO @HSData2 VALUES
          ('Sophomores',@A10,@B10,@C10)
        , ('Juniors',   @A11,@B11,@C11)
        , ('Seniors',   @A12,@B12,@C12)
        , ('Total',     @A10+@A11+@A12,@B10+@B11+@B12,@C10+@C11+@C12)

SELECT * FROM @HSData2

但似乎应该有更好的方法。

理想情况下,我可以将此全部纳入SSRS报告,然后不用担心。但它仍然让我觉得有点奇怪,我找不到任何更简单的代码。

2 个答案:

答案 0 :(得分:1)

我认为 UNPIVOT 数据要容易得多,让SSRS中的 MATRIX 担心将它重新组合在一起。

SELECT U.SCHOOL, U.CLASS, U.TOTALS
FROM TABLE1 S
UNPIVOT
(
  TOTALS
  FOR CLASS IN (SOPHMORES, JUNIORS, SENIORS)
) U;

对于Matrix,行组学校列组

您还可以使用学校制作行组的另一个矩阵,并使用班级制作列组相同的数据。

答案 1 :(得分:1)

首先取消数据,然后转动。

SELECT  * 
FROM    @HSData
UNPIVOT (
    Students 
    FOR Class IN (Sophomores, Juniors, Seniors, Total)
) up
PIVOT (
    SUM(Students)
    FOR School IN ([Armstrong HS], [Buchanan HS], [Carlsbad HS])
) p 
ORDER BY CASE Class WHEN 'Sophomores' THEN 1
                    WHEN 'Juniors' THEN 2
                    WHEN 'Seniors' THEN 3
                    WHEN 'Total' THEN 4 END