tsql:透视具有多个度量的表

时间:2017-06-19 10:38:08

标签: sql tsql pivot-table measures

这是我第一次尝试转动表格。我设法只使用一个度量来旋转表,并且无法使用多个度量来执行此操作。我能得到一些建议吗?以下是我的测试数据,我希望看到。请注意,我有大约20到30个措施。

提前致谢!

-- Test Data HAVE
CREATE TABLE #have
(Name VARCHAR(50),Subject Varchar(20), Marks1 INT,Marks2 INT, Result Varchar(20) )
GO

INSERT INTO #have VALUES('Jsmith','Maths',65,56,'Pass')
INSERT INTO #have VALUES('Jsmith','Science',42,72,'Failed')
GO

-- Test Data WANT
CREATE TABLE #want
(Name VARCHAR(50),Maths_Marks1 INT ,Maths_Marks2 INT,Science_Marks1 INT,Science_Marks2 INT,  Maths_Result Varchar(20), Science_Result Varchar(20))
GO

INSERT INTO #want VALUES('Jsmith',65,56,42,72,'Pass','Failed')
GO

select * from #have
select * from #want


-- Pivot table

SELECT Name , [Maths] AS [Maths_Marks1], [Science] AS [Science_Marks1] 
FROM 
(SELECT Name, Subject, Marks1 FROM #have) as SourceTable
PIVOT
(SUM(Marks1)
FOR Subject in ([Maths],[Science])
) as PivotTable1

 -- Also Tried..Unable to get it working
  SELECT Name , [Maths] AS [Maths_Marks1], [Science] AS [Science_Marks1] 
 FROM 
 (SELECT Name, Subject, Marks1 FROM #have) as SourceTable
 PIVOT
 (SUM(Marks1)
 FOR Subject in ([Maths])
  ) as PivotTable1
 (SELECT Name, Subject, Marks2 FROM #have) as SourceTable
PIVOT 
(SUM(Marks2)
FOR Subject in ([Science])
 ) as PivotTable2

2 个答案:

答案 0 :(得分:1)

这个技巧建立在Marks属性上:它们是整数,标记< 1000,表中恰好有2个标记列(Marks1,Marks2)。

CREATE TABLE #have
(Name VARCHAR(50),Subject Varchar(20), Marks1 INT,Marks2 INT )


INSERT INTO #have VALUES('Jsmith','Maths',65,56)
INSERT INTO #have VALUES('Jsmith','Science',42,72)

SELECT Name, [Maths]/1000 AS [Maths_Marks1],  [Maths]%1000 AS [Maths_Marks2], [Science]/1000 AS [Science_Marks1], [Science]%1000 AS [Science_Marks2] 
FROM (SELECT Name, Subject, 1000*Marks1 + Marks2 Marks FROM #have) t
PIVOT (SUM(Marks)FOR Subject in ([Maths],[Science]) ) as p1

否则,通常使用条件聚合

SELECT Name
  , max(case Subject when 'Maths' then Marks1 end ) AS [Maths_Marks1]
  , max(case Subject when 'Maths' then Marks2 end ) AS [Maths_Marks2] 
  , max(case Subject when 'Maths' then Result end ) AS [Maths_Result] 
  , max(case Subject when 'Science' then Marks1 end ) AS [Science_Marks1]
  , max(case Subject when 'Science' then Marks2 end ) AS [Science_Marks2] 
  , max(case Subject when 'Science' then Result end ) AS [Science_Result] 
FROM  #have
GROUP BY name

答案 1 :(得分:0)

您可以尝试使用枢轴,如下所示:

Select [Name], Max(Maths_Marks1) as Maths_Marks1, Max(Maths_Marks2) as Maths_Marks2, max([Science_Marks1]) as [Science_Marks1]
            ,max([Science_Marks2]) as [Science_Marks2], max([Maths_Result]) as [Maths_Result], max([Science_Result]) as [Science_Result]
    from (
    Select [Name], [Subject] + '_Marks1' as [Subject1], [Subject] +'_Marks2' as [Subject2], Marks1, Marks2, Result, [Subject] +'_Result' as [SubjectRes] from #have ) a
    pivot (max(marks1) for [Subject1] in ([Maths_Marks1],[Science_Marks1])) p
    pivot (max(marks2) for [Subject2] in ([Maths_Marks2],[Science_Marks2])) p1
    pivot (max(Result) for [SubjectRes] in ([Maths_Result],[Science_Result])) p2
group by [Name]