如何排列数据透视表的列 table
How to line up columns of a pivot table
我有一个像这样创建的枢轴table:
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%
我希望它作为支点出现 table,像这样:
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%
如果您只有这 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
编辑:包括 Jason 关于 "union all"
的建议
可能您需要 unpivot
而不是旋转。我将使用 cross apply
和 tables valued constructor
.
来做到这一点
性能方面,如果你有更多 names
,这将比 Union All
更好
SELECT [date],NAME, uptime, downtime
FROM Yourtable
CROSS apply (VALUES ('service',Services_Uptime,Services_Downtime),
('center',Centers_Uptime,Centers_Downtime) )
cs (NAME, uptime, downtime)
我有一个像这样创建的枢轴table:
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%
我希望它作为支点出现 table,像这样:
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%
如果您只有这 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
编辑:包括 Jason 关于 "union all"
的建议可能您需要 unpivot
而不是旋转。我将使用 cross apply
和 tables valued constructor
.
性能方面,如果你有更多 names
Union All
更好
SELECT [date],NAME, uptime, downtime
FROM Yourtable
CROSS apply (VALUES ('service',Services_Uptime,Services_Downtime),
('center',Centers_Uptime,Centers_Downtime) )
cs (NAME, uptime, downtime)