如何将相应的列添加到PIVOT数据集

时间:2016-09-16 04:58:20

标签: sql-server tsql pivot

使用Microsoft SQL Server 2012.

我有一张桌子:

PartID |TypeSet | Setting | ObservID |TransDate
---------------------------------------------
 1         A    456       12    10/20/2015
 1         A    377       12    10/20/2015
 1         A    425       12    10/20/2015
 1         A    665       12    10/20/2015
 1         A    543       12    10/20/2015
 1         A    554       12    10/20/2015
 1         B    34        41    10/21/2015
 1         B    27        41    10/21/2015
 1         B    27        41    10/21/2015
 1         B    29        41    10/21/2015
 1         C    1299      12    10/20/2015
 1         C    1227      12    10/20/2015

我想将其显示如下。在PartID上分组的单行。

PartID | A Avg | B Avg | C Avg |  A ObservID | B ObservID | C ObservID
 ---------------------------------------------------------------------
  1      503      29     1263        12             41         12

我不能让ObservID显示没有创建多行返回数据集。使用此查询,我对A,B和C平均值没有任何问题:

  SELECT    
      PartID
      ,[A] as 'A Average'
      ,[B] as 'B Average'   
      ,[C] as 'C Average'
     -- ,(ObservID)
     --,CASE WHEN [A] = 'A Average' THEN max([ObservID]) END as 'A Ob'
     --,CASE WHEN [B] = 'B Average' THEN max([ObservID]) END as 'B Ob'
     -- ,CASE WHEN [C] = 'C Average' THEN max([ObservID]) END as 'C Ob'
FROM
(SELECT
    PartID  
    ,TypeSet
    ,Setting
    --,ObservID
    FROM  #Temp1
    ) as MeasData
PIVOT
(
    AVG(Setting) 
    FOR [TypeSet] in 
        ( [A], [B], [C])    
) as PivotTable

您可以在上面的查询中看到我试图在其中获取ObservID,但是当我取消注释这些行时,会发生错误。 一些注意事项:ObservID将为每个TypeSet保持一致。

以下是数据:

create table #Temp1
(
  PartID INT
, TypeSet VARCHAR(10) 
, Setting INT
, ObservID INT
, TransDate Date
)

INSERT INTO #Temp1(PartID, TypeSet, Setting, ObservID, TransDate) VALUES
  (1, 'A', 456, 12, '10/20/2015'),
  (1, 'A', 377, 12, '10/20/2015'),
  (1, 'A', 425, 12, '10/20/2015'),
  (1, 'A', 665, 12, '10/20/2015'),
  (1, 'A', 543, 12, '10/20/2015'),
  (1, 'A', 554, 12, '10/20/2015'),
  (1, 'B', 34, 41, '10/21/2015'),
  (1, 'B', 27, 41, '10/21/2015'),
  (1, 'B', 27, 41, '10/21/2015'),
  (1, 'B', 29, 41, '10/21/2015'),
  (1, 'C', 1299, 12, '10/20/2015'),
  (1, 'C', 1227, 12, '10/20/2015')

PIVOT操作可以实现我想要的输出吗?或者我应该为每个Typeset在桌面上使用多个JOIN的路径?

2 个答案:

答案 0 :(得分:3)

您可以使用条件聚合执行此操作:

SELECT PartID,
     AVG(CASE WHEN TypeSet = 'A' THEN Setting END) AS 'A Avg',
     AVG(CASE WHEN TypeSet = 'B' THEN Setting END) AS 'B Avg',
     AVG(CASE WHEN TypeSet = 'C' THEN Setting END) AS 'C Avg',
     MAX(CASE WHEN TypeSet = 'A' THEN ObservID END) AS 'A ObservID',
     MAX(CASE WHEN TypeSet = 'B' THEN ObservID END) AS 'B ObservID',
     MAX(CASE WHEN TypeSet = 'C' THEN ObservID END) AS 'C ObservID'
FROM #Temp1
GROUP BY PartID

答案 1 :(得分:0)

  WITH avgPivot AS
(
  SELECT 
      PartID,[a] as A_Avg,[b] as B_Avg,[c] as C_Avg
  FROM
  (
    SELECT PartID, TypeSet, Setting, ObservID
    FROM #Temp1
  ) As S
  PIVOT
  (
     AVG(Setting)
     FOR TypeSet in([a],[b],[c])
  )
  AS pvt
),
maxPivot AS
(
  SELECT 
       [a] as A_ObservID, [B]B_ObservID, [c] as c_ObservID
  FROM
  (
    SELECT PartID, TypeSet, Setting, ObservID
    FROM #Temp1
  ) As S
  PIVOT
  (
     max(ObservID)
     FOR TypeSet in([a],[b],[c])
  )
  AS pvt
)

SELECT distinct *
FROM avgPivot M
 cross JOIN maxPivot A