透视或透视

时间:2014-03-29 01:01:21

标签: sql-server pivot-table unpivot

我有一个包含每小时统计数据的表格,我需要转动(或取消忽略?)

起始表的格式为:

[SystemID] [Hour] [CallStarts] [AvgDuration]  
    1         0        3           27
    1         1       10           58
    1         2       43           45
    1         3       54           63
    2         0        6           56
    2         1       46           98
    2         2       56           67
    2         3       65           77  

我需要输出:

[SystemID] [Statistic] [Hr0] [Hr1] [Hr2] [Hr3]  
     1     CallStarts    3    10     43    54  
     1     AvgDuration  27    58     45    63
     2     CallStarts    6    46     56    65
     2     AvgDuration  56    98     67    77  

我使用的是SQL Server 2008R2。我理解数据透视表的基础知识,但我没有多少经验。它的[统计]专栏让我感到茫然。

3 个答案:

答案 0 :(得分:1)

你有正确的想法......忽略然后转动。也许有更好的方法,但这有效:

SELECT *
FROM (
    SELECT SystemId, [Hour], [Stat], [Statistics]
    FROM
        (SELECT systemid, [hour], CallStarts, AvgDuration FROM t) nt
    UNPIVOT
        (stat FOR [Statistics] IN (CallStarts, AvgDuration)) AS ut
    ) AS ap
PIVOT
    (SUM (Stat) FOR [Hour] IN ([0], [1], [2], [3], [4])) AS p
ORDER BY [SystemId], [Statistics] DESC

这是一个显示正常工作的方形工具:http://www.sqlfiddle.com/#!3/eb109/8/0

答案 1 :(得分:1)

您可以单独查询每种类型的统计信息,然后将它们联合起来。我使用了@attila在SQL Fiddle上构建的相同模式。

WITH callS AS (SELECT systemid, hour, callstarts FROM t),
callD AS (SELECT systemid, hour, avgduration FROM t)

SELECT systemid, 'callStarts' AS [statistics], [0] as Hr0, [1] as Hr1, [2] as Hr2, [3] AS Hr3
FROM callS pivot (max(callStarts) FOR hour IN ([0], [1], [2], [3])) AS p

UNION ALL

SELECT systemid, 'AvgDuration' AS [statistics], [0], [1], [2], [3]
FROM callD pivot (max(avgduration) FOR hour IN ([0], [1], [2], [3])) AS p2

ORDER BY systemid, [statistics] DESC;

可以在此处看到输出:http://www.sqlfiddle.com/#!3/eb109/20/0

答案 2 :(得分:1)

这是一种在没有数据透视的情况下获得这些结果的方法,具体取决于索引和数据大小,它可能更快:

SELECT SystemID, 
       MAX('CallStarts') AS Statistic, 
       SUM(CASE WHEN Hour = 0 THEN CallStarts ELSE 0 END) AS Hr0,
       SUM(CASE WHEN Hour = 1 THEN CallStarts ELSE 0 END) AS Hr1,
       SUM(CASE WHEN Hour = 2 THEN CallStarts ELSE 0 END) AS Hr2,
       SUM(CASE WHEN Hour = 3 THEN CallStarts ELSE 0 END) AS Hr3
FROM Table
GROUP BY SystemID
  UNION ALL
SELECT SystemID, 
       MAX('AvgDuration') AS Statistic, 
       SUM(CASE WHEN Hour = 0 THEN AvgDuration ELSE 0 END) AS Hr0,
       SUM(CASE WHEN Hour = 1 THEN AvgDuration ELSE 0 END) AS Hr1,
       SUM(CASE WHEN Hour = 2 THEN AvgDuration ELSE 0 END) AS Hr2,
       SUM(CASE WHEN Hour = 3 THEN AvgDuration ELSE 0 END) AS Hr3
FROM Table
GROUP BY SystemID
ORDER BY SystemID, Statistic ASC