0

I'd like to know if it is possible to translate the table below:

enter image description here

into a pivot table like this?

enter image description here

i tried the query below but it is not giving me the right results:

SELECT * FROM
(
    select 
        WorkWeek
        , CometsFT
        , CometsTR
        , CSP
        , MaxCIMFT
        , MaxCIMTR
        , MaxCIMWS
        , STD
    from tblLotTrackingDetails
) s
pivot
(
    sum(cometsft)
    for WorkWeek in (WW6, WW7, WW8, WW9, WW10)
)as piv
4

2 回答 2

1

你必须UNPIVOT先到你的桌子然后PIVOT

WITH unpivot_details AS
(
  SELECT WorkWeek, Type, Value
    FROM 
  ( 
    SELECT WorkWeek, CometsFT, CometsTR, CSP, MaxCIMFT, MaxCIMTR, MaxCIMWS, STD
      FROM tblLotTrackingDetails 
  ) s
  UNPIVOT
  ( 
    Value FOR Type IN (CometsFT, CometsTR, CSP, MaxCIMFT, MaxCIMTR, MaxCIMWS, STD) 
  ) s
)
SELECT Type RowLabels, 
       WW1, WW2, WW3, WW4, WW5, WW6, WW7, WW8, WW9, WW10
  FROM
(
  SELECT WorkWeek, Type, Value
    FROM unpivot_details
  UNION ALL
  SELECT WorkWeek, 'Total', SUM(Value) Value
    FROM unpivot_details
   GROUP BY WorkWeek
) s
PIVOT
(
  SUM(Value) FOR WorkWeek IN(WW1, WW2, WW3, WW4, WW5, WW6, WW7, WW8, WW9, WW10)
) p
 ORDER BY CASE WHEN Type = 'Total' THEN 1 ELSE 0 END,
          Type

样本输出:

| 行标签 | 第一次世界大战 | 二战 | 二战 | 二战 | 二战 | 二战 | 二战 | 二战 | 二战 | WW10 |
|-----------|-----|-----|-----|-----|-----|-----|- ----|-----|-----|------|
| 彗星FT | 42 | 50 | 30 | 45 | 25 | 36 | 26 | 33 | 35 | 35 |
| 彗星TR | 20 | 14 | 30 | 28 | 24 | 14 | 21 | 18 | 21 | 9 |
| 太阳能电池板 | 6 | 4 | 1 | 7 | 9 | 8 | 8 | 2 | 4 | 3 |
| 最大CIMFT | 52 | 58 | 105 | 77 | 42 | 39 | 34 | 23 | 34 | 19 |
| 最大CIMTR | 21 | 2 | 7 | 9 | 34 | 4 | 5 | 7 | 7 | 9 |
| 最大CIMWS | 28 | 29 | 59 | 82 | 24 | 26 | 19 | 20 | 40 | 21 |
| 性病 | 3 | 40 | 2 | 8 | 2 | 5 | 5 | 2 | 9 | 2 |
| 总计 | 172 | 197 | 234 | 256 | 160 | 132 | 118 | 105 | 150 | 98 |

这是SQLFiddle演示


根据您的评论进行更新。此查询将提取第 11 周到第 15 周的数据。

WITH unpivot_details AS
(
  SELECT WorkWeek, Type, Value
    FROM 
  ( 
    SELECT WorkWeek, CometsFT, CometsTR, CSP, MaxCIMFT, MaxCIMTR, MaxCIMWS, STD
      FROM tblLotTrackingDetails 
     WHERE WorkWeek IN ('WW11', 'WW12', 'WW13', 'WW14', 'WW15')
  ) s
  UNPIVOT
  ( 
    Value FOR Type IN (CometsFT, CometsTR, CSP, MaxCIMFT, MaxCIMTR, MaxCIMWS, STD) 
  ) s
)
SELECT Type RowLabels, 
       WW11, WW12, WW13, WW14, WW15
  FROM
(
  SELECT WorkWeek, Type, Value
    FROM unpivot_details
  UNION ALL
  SELECT WorkWeek, 'Total', SUM(Value) Value
    FROM unpivot_details
   GROUP BY WorkWeek
) s
PIVOT
(
  SUM(Value) FOR WorkWeek IN(WW11, WW12, WW13, WW14, WW15)
) p
 ORDER BY CASE WHEN Type = 'Total' THEN 1 ELSE 0 END,
          Type

这是SQLFiddle演示

于 2013-09-14T04:38:34.127 回答
0

最后,经过多次尝试,我能够做我想做的事。彼得姆的建议奏效了。为了分享,下面是我将使用的动态枢轴:

DECLARE @Columns NVARCHAR(MAX)
        ,@query NVARCHAR(MAX)
SELECT @Columns = STUFF(
 (SELECT  ', ' +'['+WorkWeek+']' FROM
 (SELECT TOP 10 WorkWeek FROM(SELECT TOP 10 WorkWeek , LotTrackingID    FROM tblLotTracking ORDER BY LotTrackingID DESC) s Order by s.LotTrackingID) AS T FOR XML PATH('')),1,2,'') 

SET @query = N'WITH unpivot_details AS
(
  SELECT WorkWeek, Type, Value
    FROM 
  ( 
    SELECT WorkWeek, CometsFT, CometsTR, CSP, MaxCIMFT, MaxCIMTR, MaxCIMWS, STD
      FROM tblLotTrackingDetails 
  ) s
  UNPIVOT
  ( 
    Value FOR Type IN (CometsFT, CometsTR, CSP, MaxCIMFT, MaxCIMTR, MaxCIMWS, STD) 
  ) s
)
SELECT Type RowLabels, 
       ' + @Columns + '
  FROM
(
  SELECT WorkWeek, Type, Value
    FROM unpivot_details
  UNION ALL
  SELECT WorkWeek, ''Total'', SUM(Value) Value
    FROM unpivot_details
   GROUP BY WorkWeek
) s
PIVOT
(
  SUM(Value) FOR WorkWeek IN(' + @Columns + ')
) p
 ORDER BY CASE WHEN Type = ''Total'' THEN 1 ELSE 0 END,
          Type';
EXEC sp_executesql @query;
于 2013-09-15T11:08:58.733 回答