旋转多列的数据

时间:2013-09-23 09:19:44

标签: sql-server sql-server-2008 tsql pivot

我有一个如下数据表:

Name        MetricName         Value    RecievedPoints  MaxPoints
-----------------------------------------------------------------
Birchwood   FirstManualRespTime 80      0                   30
Birchwood   FirstPhoneRespTime  n/a     n/a                 10
Birchwood   FirstPricedRespTime n/a     n/a                 10
Birchwood   FollowUPEmlCnt      8       n/a                 20
Birchwood   QuotedPrice         n/a     0                   10
Birchwood   TotPhCallsCnt       0       n/a                 10
Jim         FirstManualRespTime 65      0                   30
Jim         FirstPhoneRespTime  n/a     n/a                 10
Jim         FirstPricedRespTime n/a     n/a                 10
Jim         FollowUPEmlCnt      3       n/a                 20
Jim         QuotedPrice         n/a     0                   10
Jim         TotPhCallsCnt       0       n/a                 10

是否可以获得如下数据?

Name FirstManualRespTime RecievedPoints MaxPoints FirstPhoneRespTime RecievedPoints MaxPoints FirstPricedRespTime RecievedPoints MaxPoints FollowUPEmlCnt RecievedPoints MaxPoints QuotedPrice RecievedPoints MaxPoints TotPhCallsCnt RecievedPoints MaxPoints                  
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Birchwood   80           0                30         n/a              n/a            10                 n/a              n/a         10           8            n/a          20          n/a            0      10          0              n/a          10

其他客户也一样。

2 个答案:

答案 0 :(得分:1)

复制这个确切的脚本并执行它:

declare @t table(Name varchar(12), MetricName varchar(25), Value int, ReceivedPoints int, MaxPoints int)
insert @t values('Birchwood','FirstManualRespTime', 80,'0','30'),('Birchwood','FirstPhoneRespTime', null,null,'10')
,('Birchwood','FirstPricedRespTime', null,null,'10'),('Birchwood','FollowUPEmlCnt','8',null,'20')
,('Birchwood','QuotedPrice',null,'0','10'),('Birchwood','TotPhCallsCnt','0',null,'10')
,('Jim','FirstManualRespTime', 65,'0','30'),('Jim','FirstPhoneRespTime', null,null,'10')
,('Jim','FirstPricedRespTime', null,null,'10'),('Jim','FollowUPEmlCnt','3',null,'20')
,('Jim','QuotedPrice',null,'0','10'),('Jim','TotPhCallsCnt','0',null,'10')

;with a as
(
select name, MetricName x, value y from @t
union all select name, 'ReceivedPoints1' x, ReceivedPoints from @t where MetricName = 'FirstManualRespTime'
union all select name, 'MaxPoints1', MaxPoints from @t where MetricName = 'FirstManualRespTime'
union all select name, 'ReceivedPoints2' x, ReceivedPoints from @t where MetricName = 'FirstPhoneRespTime'
union all select name, 'MaxPoints2', MaxPoints from @t where MetricName = 'FirstPhoneRespTime'
union all select name, 'ReceivedPoints3' x, ReceivedPoints from @t where MetricName = 'FirstPricedRespTime'
union all select name, 'MaxPoints3', MaxPoints from @t where MetricName = 'FirstPricedRespTime'
union all select name, 'ReceivedPoints4' x, ReceivedPoints from @t where MetricName = 'FollowUPEmlCnt'
union all select name, 'MaxPoints4', MaxPoints from @t where MetricName = 'FollowUPEmlCnt'
union all select name, 'ReceivedPoints5' x, ReceivedPoints from @t where MetricName = 'QuotedPrice'
union all select name, 'MaxPoints5', MaxPoints from @t where MetricName = 'QuotedPrice'
union all select name, 'ReceivedPoints6' x, ReceivedPoints from @t where MetricName = 'TotPhCallsCnt'
union all select name, 'MaxPoints6', MaxPoints from @t where MetricName = 'TotPhCallsCnt'

)
select [name],
  [FirstManualRespTime],[ReceivedPoints1] ReceivedPoints,[MaxPoints1] MaxPoints,
  [FirstPhoneRespTime] ,[ReceivedPoints2] ReceivedPoints,[MaxPoints2] MaxPoints,
  [FirstPricedRespTime],[ReceivedPoints3] ReceivedPoints,[MaxPoints3] MaxPoints,
  [FollowUPEmlCnt]     ,[ReceivedPoints4] ReceivedPoints,[MaxPoints4] MaxPoints,
  [QuotedPrice]        ,[ReceivedPoints5] ReceivedPoints,[MaxPoints5] MaxPoints
from a
PIVOT (sum(y)  
for x
in([FirstManualRespTime],[ReceivedPoints1],[MaxPoints1],[FirstPhoneRespTime],[ReceivedPoints2],[MaxPoints2],[FirstPricedRespTime],[ReceivedPoints3],[MaxPoints3],[FollowUPEmlCnt],[ReceivedPoints4],[MaxPoints4],[QuotedPrice],[ReceivedPoints5],[MaxPoints5])  
)as p order by name

答案 1 :(得分:1)

以下是我的想法。

select @cols = STUFF((SELECT   ',' + QUOTENAME(col+cast(MetricName as varchar(100))) 
                from #procresults
                cross apply
                (
                  select 'C' union all
                  select 'RecievedPoints' union all
                  select 'MaxPoints' 
                ) c (col)
                group by col,  MetricName
                order by MetricName
        FOR XML PATH(''), TYPE
        ).value('.', 'NVARCHAR(MAX)') 
    ,1,1,'')

set @query = 'SELECT Name,' + @cols + ' 
        from 
        (
            select Name, col+cast(MetricName as varchar(50)) col,  val
            from #procresults
            cross apply
            (
              select ''C'', Value union all
              select ''RecievedPoints'', convert(varchar(10), RecievedPoints, 120) union all
              select ''MaxPoints'', convert(varchar(10), MaxPoints, 120)
            ) c (col, val)
        ) x
        pivot 
        (
            max(val)
            for col in (' + @cols + ')
        ) p '

 execute sp_executesql @query