Sql 如何排列数据透视表的列
我有一个正在创建的透视表,如下所示:Sql 如何排列数据透视表的列,sql,sql-server,database,pivot,pivot-table,Sql,Sql Server,Database,Pivot,Pivot Table,我有一个正在创建的透视表,如下所示: date Services Uptime Services Downtime Centers Downtime Centers Uptime ----- --------- - ------------------ ---------------- --------------- 12/5/14 100.00% 0.00% 100.00%
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%
我希望它能成为一个数据透视表,如下所示:
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%
如果只有这两个值,请尝试联合:
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关于“联合所有”的建议,请尝试联合:
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关于“联合所有”的建议,请尝试联合:
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关于“联合所有”的建议,请尝试联合:
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关于“联合所有”的建议,可能需要
取消PIVOT
,而不是旋转。我将使用交叉应用
和表值构造函数
来实现这一点
性能方面,如果您有更多的名称
SELECT [date],NAME, uptime, downtime
FROM Yourtable
CROSS apply (VALUES ('service',Services_Uptime,Services_Downtime),
('center',Centers_Uptime,Centers_Downtime) )
cs (NAME, uptime, downtime)
可能您需要
unpivot
而不是旋转。我将使用交叉应用
和表值构造函数
来实现这一点
性能方面,如果您有更多的名称
SELECT [date],NAME, uptime, downtime
FROM Yourtable
CROSS apply (VALUES ('service',Services_Uptime,Services_Downtime),
('center',Centers_Uptime,Centers_Downtime) )
cs (NAME, uptime, downtime)
可能您需要
unpivot
而不是旋转。我将使用交叉应用
和表值构造函数
来实现这一点
性能方面,如果您有更多的名称
SELECT [date],NAME, uptime, downtime
FROM Yourtable
CROSS apply (VALUES ('service',Services_Uptime,Services_Downtime),
('center',Centers_Uptime,Centers_Downtime) )
cs (NAME, uptime, downtime)
可能您需要
unpivot
而不是旋转。我将使用交叉应用
和表值构造函数
来实现这一点
性能方面,如果您有更多的名称
SELECT [date],NAME, uptime, downtime
FROM Yourtable
CROSS apply (VALUES ('service',Services_Uptime,Services_Downtime),
('center',Centers_Uptime,Centers_Downtime) )
cs (NAME, uptime, downtime)
虽然这并没有错,但使用
union all
而不是union
可以(取决于表、索引等)在这种情况下(在这种情况下,无论如何都不会有任何重复的行)显著提高性能。谢谢你,但是我有6个以上这样的值。你能帮我吗。建议我一个最好的方法。虽然这没有错,但使用union all
而不是union
可以(取决于表、索引等)在这样的情况下(在这种情况下,无论如何都不会有任何重复行)显著提高性能。谢谢你,但我有6个以上类似的值。你能帮我吗。建议我一个最好的方法。虽然这没有错,但使用union all
而不是union
可以(取决于表、索引等)在这样的情况下(在这种情况下,无论如何都不会有任何重复行)显著提高性能。谢谢你,但我有6个以上类似的值。你能帮我吗。建议我一个最好的方法。虽然这没有错,但使用union all
而不是union
可以(取决于表、索引等)在这样的情况下(在这种情况下,无论如何都不会有任何重复行)显著提高性能。谢谢你,但我有6个以上类似的值。你能帮我吗。建议我一个最好的方法。谢谢你更好的答案。谢谢你更好的答案。谢谢你更好的答案。谢谢你更好的答案。