2015-02-23 102 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