Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 多个列的透视数据_Sql Server_Sql Server 2008_Tsql_Pivot - Fatal编程技术网

Sql server 多个列的透视数据

Sql server 多个列的透视数据,sql-server,sql-server-2008,tsql,pivot,Sql Server,Sql Server 2008,Tsql,Pivot,我有如下数据表: Name MetricName Value RecievedPoints MaxPoints ----------------------------------------------------------------- Birchwood FirstManualRespTime 80 0 30 Birchwood FirstPhoneRespTime n/a n/a

我有如下数据表:

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

对其他客户也是如此。

复制此脚本并执行:

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

下面是我想到的

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

你测试过我的脚本吗?在我得到这个答案之前,我已经用不同的方式处理了它(参见我的更新)。无论如何,谢谢你的帮助!!我也投你的票