2015-02-23 67 views
0

我有被这样创建的数据透视表数据透视表的列:如何排队

date Services Uptime  Services Downtime Centers Downtime Centers Uptime 
----- --------- -  ------------------ ---------------- --------------- 
12/5/14  100.00%    0.00%     100.00%  100.00%  
12/12/14  100.00%    0.00%      0.00%   0.00% 
12/19/14  100.00%    0.00%     100.00%  0.00% 
12/26/14  100.00%    0.00%     100.00%   0.00% 

我想它出来的数据透视表,就像这样:

Date   Name    Uptime    Downtime 
-----  ------   ---------   ------------- 
12/5/14  Services    100.00%     0.00% 
12/5/14  Center    100.00%    100.00% 
12/12/14 services    100.00%     0.00% 
12/12/14 Center     0.00%     0.00% 

回答

0

可能是您需要unpivot而不是pivoting。我会用cross applytables valued constructor来做到这一点。

明智的性能,这将是比Union All如果你有更多的names

SELECT [date],NAME, uptime, downtime 
FROM Yourtable 
     CROSS apply (VALUES ('service',Services_Uptime,Services_Downtime), 
          ('center',Centers_Uptime,Centers_Downtime)) 
        cs (NAME, uptime, downtime) 
+0

感谢您的更好回答。 – Binny 2015-02-23 18:22:28

3

如果只有那些2倍的值,尝试UNION:

select [date] 
     ,'Services' 
     ,[Services Uptime] as Uptime 
     ,[Services Dowtime] as Downtime 
from myTable 
union all 
select [date] 
     ,'Center' 
     ,[Centers Uptime] as Uptime 
     ,[Centers Dowtime] as Downtime 
from myTable 

编辑:包括贾森建议,关于“U nion 全部

+3

虽然这是没有错的,使用'工会all'取代'union'可以(根据表,索引等)在像这样的情况下大幅提升性能(无论如何你都不会有任何重复的行)。 – 2015-02-23 16:16:00

+1

@JackManey你是对的+1 – Rubik 2015-02-23 16:18:44

+0

谢谢男人,但我有超过6个这样的值。你可以请。建议我一个最好的方法。 – Binny 2015-02-23 17:39:48