【问题标题】:Creating a dynamic pivot with the headers as the first row创建以标题为第一行的动态枢轴
【发布时间】:2013-11-03 21:15:55
【问题描述】:

我已经创建了一个动态枢轴的过程,以便我可以总结一个项目的工作时间。在这个枢轴上,我想在顶部插入一行,其中包含列标题。

这就是表格的样子

headers --> |  Name  |  Hours  |  Project  |
            |  Bill  |    30   |   proj1   |
            |  Steve |    34   |   proj1   |
            |  Bill  |    42   |   proj2   |

这就是枢轴的样子

headers --> |  Name  |  proj1  |  proj2  |
            |  Bill  |    30   |    42   |
            |  Steve |    34   |         |

这就是我想要的样子

headers --> | column1 | column2 | column3 |
            |  Name   |  proj1  |  proj2  |
            |  Bill   |    30   |    42   |
            |  Steve  |    34   |         |

有没有办法通过创建临时表或其他方式来做到这一点? 是的,我需要将标题作为一行,而不仅仅是列标题。

【问题讨论】:

  • Name,proj1,proj2,..projn 本身是列标题,而不是包含所需数据的行。你能告诉我们更多你想要做什么吗?

标签: tsql pivot union pivot-table


【解决方案1】:

通常,当我需要时,我会将包含文本的选择与需要标题行的查询合并。为了让它们登上榜首,您需要通过将它们放在首位的东西来订购。如果您没有任何内容,请添加一个假字段。

select 0 'orderby', col1, col2, col3 
from mytable
union
select -1, 'col1', 'col2', 'col3'
order by orderby

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-26
    相关资源
    最近更新 更多