Planet For Application Life Development Presents
MY IT World

Explore and uptodate your technology skills...

SQL PIVOT Clause

Microsoft SQL Server has introduced the PIVOT and UNPIVOT commands as enhancements to T-SQL with the release of Microsoft SQL Server 2005.

In Microsoft SQL Server 2008, we can still use the PIVOT command and UNPIVOT commands to build and use pivot tables in SQL.

T-SQL, Pivot and Unpivot statements will transform and rotate a tabular data into another table value data in SQL.

It is used to generate cross tabulation reports to summarize data as it creates a more easily understandable data in a user friendly format.

PIVOT rotates a table-valued expression by turning the unique values from one column in the expression into multiple columns in the output, i.e., it rotates rows to columns and aggregations where they are required on any remaining column values that are wanted in the final output.

SELECT Item, sum(users),
max([Date]),
[Document],[Extraction] FROM Table
PIVOT
( sum([Count]) FOR [Type] IN ( Document , Extraction)) AS [QUANTITY]
GROUP BY activity, document, extraction