我有一个支点表,它是这样创建的:
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%发布于 2015-02-23 17:38:46
也许你需要unpivot而不是旋转。我将使用cross apply和tables 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) 发布于 2015-02-23 16:14:58
如果您只有这两个值,请尝试一个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关于“all__”的建议
https://stackoverflow.com/questions/28677905
复制相似问题