首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何排列枢轴表的列

如何排列枢轴表的列
EN

Stack Overflow用户
提问于 2015-02-23 16:02:03
回答 2查看 169关注 0票数 0

我有一个支点表,它是这样创建的:

代码语言:javascript
运行
复制
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%

我希望它作为一个枢轴表出来,就像这样:

代码语言:javascript
运行
复制
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%
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-02-23 17:38:46

也许你需要unpivot而不是旋转。我将使用cross applytables valued constructor来完成这个任务。

就性能而言,如果您有更多的Union All,这将比names更好

代码语言:javascript
运行
复制
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
EN

Stack Overflow用户

发布于 2015-02-23 16:14:58

如果您只有这两个值,请尝试一个UNION:

代码语言:javascript
运行
复制
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__”的建议

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28677905

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档