【问题标题】:T-SQL pivot with more then one non-pivot columns具有多于一个非枢轴列的 T-SQL 枢轴
【发布时间】:2012-09-23 01:29:28
【问题描述】:

我有几列并制作支点。我想有多个非枢轴列并使用最后一个进行枢轴。在原始规范here 中显示,您只能拥有一个非枢轴列。

SELECT <non-pivoted column>,

    [first pivoted column] AS <column name>,

    [second pivoted column] AS <column name>,

    ...

    [last pivoted column] AS <column name>

FROM

    (<SELECT query that produces the data>)

    AS <alias for the source query>

PIVOT

(

    <aggregation function>(<column being aggregated>)

FOR

[<column that contains the values that will become column headers>]

    IN ( [first pivoted column], [second pivoted column],

    ... [last pivoted column])

) AS <alias for the pivot table>

<optional ORDER BY clause>;

有没有办法拥有更多的非透视列,因为它使用第一列之后的所有列来透视我的数据。

【问题讨论】:

    标签: sql-server sql-server-2008 tsql pivot


    【解决方案1】:

    是的。只需添加它们。

    例如

    declare @t table (a int, b int, c int, d int)
    insert @t values (1,2,3,4)
    insert @t values (7,6,5,3)
    
    select a,b, [3],[4] from @t s
    pivot 
    (sum(c) for d in ([3],[4])) p
    

    【讨论】:

      猜你喜欢
      • 2011-04-26
      • 2017-08-14
      • 1970-01-01
      • 1970-01-01
      • 2018-08-28
      • 2018-11-27
      • 1970-01-01
      • 2020-12-27
      • 1970-01-01
      相关资源
      最近更新 更多