(MSSQL)資料轉置PIVOT(Row2Col)

Syntax

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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>;
繼續閱讀