複製這個確切的腳本並執行它:
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
你測試了我的腳本嗎? –