UNPIVOT in SQL Server

UNPIVOT in SQL Server

sql server convert columns to rows in a table
sql server transpose columns to rows
unpivot in sql server example
sql server unpivot example

In this video we will discuss UNPIVOT operator in SQL Server.

PIVOT operator turns ROWS into COLUMNS, where as UNPIVOT turns COLUMNS into ROWS.

We dicussed PIVOT operator in Part 54 of SQL Server tutorial. Please watch Part 54 before proceeding.
http://csharp-video-tutorials.blogspot.com/2012/10/pivot-operator-in-sql-server-part-54.html

Let us understand UNPIVOT with an example. We will use the following tblProductSales table in this demo.

SQL Script to create tblProductSales table
Create Table tblProductSales
(
SalesAgent nvarchar(50),
India int,
US int,
UK int
)
Go

Insert into tblProductSales values (‘David’, 960, 520, 360)
Insert into tblProductSales values (‘John’, 970, 540, 800)
Go

Write a query to turn COLUMNS into ROWS.

SELECT SalesAgent, Country, SalesAmount
FROM tblProductSales
UNPIVOT
(
SalesAmount
FOR Country IN (India, US ,UK)
) AS UnpivotExample

Text version of the video
http://csharp-video-tutorials.blogspot.com/2015/10/unpivot-in-sql-server.html

Slides
http://csharp-video-tutorials.blogspot.com/2015/10/unpivot-in-sql-server_10.html

All SQL Server Text Articles
http://csharp-video-tutorials.blogspot.com/p/free-sql-server-video-tutorials-for.html

All SQL Server Slides
http://csharp-video-tutorials.blogspot.com/p/sql-server.html

All Dot Net and SQL Server Tutorials in English
https://www.youtube.com/user/kudvenkat/playlists?view=1&sort=dd

All Dot Net and SQL Server Tutorials in Arabic
https://www.youtube.com/c/KudvenkatArabic/playlists

Get Paid Taking Pictures
Share