2013-09-23 52 views
1

透視數據我有一個表的數據如下圖所示:超過一列

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 

其他客戶也一樣。

+0

你測試了我的腳本嗎? –

回答

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 
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 
+0

我得到這個答案之前,我處理它的方式不同(請參閱我的更新)。無論如何感謝您的幫助!我也給你的答案投票 – mmssaann