2012-10-02 22 views
2

我有幾個列並且進行了調整。我想有多個非透視列,並使用最後一個透視。在原始規範here中顯示您只能有一個非樞軸列。帶有多個非支點列的T-SQL支點

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>; 

有沒有一種方法能有更多的非關鍵列,因爲它使用的是第一個之後的所有列擺動我的數據。

回答

2

是的。只需添加它們。

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