Transforming Data with PIVOT in SQL Server: Summarize Data by Columns

preview_player
Показать описание
Learn how to use the PIVOT operator in SQL Server to transform rows into columns, making it easy to summarize and analyze data. This tutorial demonstrates how to pivot data from an Orders table, creating a report that shows total sales for each customer across different months. The PIVOT operator is perfect for turning transactional data into a clear and organized summary, ideal for reporting and analysis. Watch to master this advanced SQL technique and enhance your data manipulation skills!

Commands:
"PIVOT"

SELECT *
FROM (
SELECT CustomerID, MONTH(OrderDate) AS OrderMonth, TotalAmount
FROM Orders
) AS SourceTable
PIVOT (
SUM(TotalAmount)
FOR OrderMonth IN ([1], [2], [3])
) AS PivotTable;

"UNPIVOT"

SELECT CustomerID, OrderMonth, TotalAmount
FROM PivotTable
UNPIVOT (
TotalAmount FOR OrderMonth IN ([1], [2], [3])
) AS UnpivotTable;

#SQLServer #PIVOT #DataTransformation #SQLTutorial #DatabaseAnalysis #SQLServerAdvanced #DataSummarization #PivotSQL #SQLTips #LearnSQL
Рекомендации по теме
welcome to shbcf.ru