如何将行转换为列

时间:2016-10-25 23:33:15

标签: tsql ssrs-2008-r2

我有一个Tablix,包含Type,Default,Break,CRG,Meeting,Training,Specialist,Personal,SpecialProjects和SystemOutage是水平排列的。我需要将它放在饼图中,每个图表按类别按数据集进行过滤。我遇到的问题是Default,Break,CRG,Meeting,Training,Specialist,Personal,SpecialProjects和SystemOutage处于水平布局,我需要它处于Vertical状态。我尝试了Unpivot,但是我无法使用9个字段。见下文。

enter image description here

enter image description here 饼图所需的预期结果:

1 个答案:

答案 0 :(得分:1)

也许这就是你要找的东西

Declare @YourTable table (Type varchar(50),[Default] int,[Break] int,CRG int,Meeting int,Training int,Specialist int,Personal int,SpecialProjects int,SystemOutage int)
Insert into @YourTable values
('Category1',32721,40634,0,1348,9162,0,10212,44549,17090),
('Category2',261997,83567,0,0,552312,0,17859,59345,30138)

Select B.*
 From  (Select Type,XMLData=cast((Select A.* for XML Raw) as xml) From @YourTable A) A
 Cross Apply (
                Select Type   = r.value('@Type','varchar(50)')
                      ,Item   = attr.value('local-name(.)','varchar(100)')
                      ,Value  = attr.value('.','int') 
                 From  XMLData.nodes('/row') as A(r)
                 Cross Apply A.r.nodes('./@*') AS B(attr)
                 Where attr.value('local-name(.)','varchar(100)') not in ('Type')
             ) B
 Where A.Type='Category1'

<强>返回

enter image description here