반응형
피벗은 테이블을 효과적으로 순환하여 한 열의 고유값을 여러열로 변환하는데 사용할 수 있는 SQL server operator이다.
Pivot is a sql server operator that can be used to turn unique values from one column, into multiple columns in the output, there by effectively rotating a table.
Example,
Select SalesAgent, Korea, US, UK
from
(
Select SalesAgent, SalesCountry, SalesAmount
from tblProductSale
) as SourceTable
PIVOT
(
SUM(SalesAmount) FOR SalesCountry IN([Korea], [US], [UK])
)
AS PivotTable
Syntax from MSDN
SELECT <non-pivoted column>,
[first pivoted column] AS <column name>,
[secount pivoted column] AS <column name>,
...
[last pivoted column] AS <column name>
FROM
(<SELECT query that producess 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>
반응형
'Database > SQL Server' 카테고리의 다른 글
SQL Server - 트랜잭션 (Transaction) (0) | 2020.12.09 |
---|---|
SQL Server - 에러 처리 Error handling(@@ERROR, TRY...CATCH) (0) | 2020.12.09 |
SQL Server - 정규화, 1~3 정규형 [1NF, 2NF, 3NF] (0) | 2020.12.08 |
SQL Server - 데이터베이스 정규화 Database normalization (0) | 2020.12.08 |
SQL Server - 재귀적 공통 테이블 식 (Recursive CTE) (0) | 2020.12.08 |