Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Pivot and Unpivot

Author  Topic 

Delinda
Constraint Violating Yak Guru

315 Posts

Posted - 2010-04-07 : 06:29:26
What's the easy explanation for PIVOT and UNPIVOT?

Can i get the simple example for this both?

Asken
Starting Member

38 Posts

Posted - 2010-04-07 : 06:33:41
http://msdn.microsoft.com/en-us/library/ms177410.aspx


Reporting & Analysis Specialist
Go to Top of Page

Delinda
Constraint Violating Yak Guru

315 Posts

Posted - 2010-04-07 : 06:50:18
Let's say, i've as follow,

declare @Departments table
(DepartmentID int identity(1,1),Name varchar(30));
insert into @Departments values('D1');
insert into @Departments values('D2');
insert into @Departments values('D3');
insert into @Departments values('D4');

declare @Employees table
(EmployeeID int identity(1,1),[First Name] varchar(20),[Last Name] varchar(20),DepartmentID int);
insert into @Employees values('John','Smith',1);
insert into @Employees values('Dave','Johnson',2);
insert into @Employees values('Mark','Andrew',2);
insert into @Employees values('George','Michael',4);
insert into @Employees values('Michael','Peterson',3);

select DepartmentName,NoOfEmployee
from
(select count(t1.EmployeeID) as NoOfEmployee, t2.Name as DepartmentName
from @Employees t1 inner join @Departments t2
on t1.DepartmentID=t2.DepartmentID
group by t1.DepartmentID,t2.Name)t1 where NoOfEmployee < 10

Output
DepartmentName | NoOfEmployee
---------------------
D1 1
D2 2
D3 1
D4 1


How to use the SQL using PIVOT, and SQL using UNPIVOT based on result set above?
Go to Top of Page

DBA in the making
Aged Yak Warrior

638 Posts

Posted - 2010-04-07 : 06:52:08
Books Online has an example for both. From BOL:

USE AdventureWorks
GO
SELECT VendorID, [164] AS Emp1, [198] AS Emp2, [223] AS Emp3, [231] AS Emp4, [233] AS Emp5
FROM
(SELECT PurchaseOrderID, EmployeeID, VendorID
FROM Purchasing.PurchaseOrderHeader) p
PIVOT
(
COUNT (PurchaseOrderID)
FOR EmployeeID IN
( [164], [198], [223], [231], [233] )
) AS pvt
ORDER BY VendorID;

Here, SQL Server returns a column VenderID, and one column for each of the 5 EmployeeIDs found in the Purchasing.PurchaseOrderHeader.EmployeeID column. Each of these columns is populated with a count of PurchaseOrderIDs for that VendorID/EmployeeID. The pivot function pivots the data. It converts row data (in this case, the EmployeeID) into columns. Compare the above query with it's non-pivoted equivalent, and you will see how the PIVOT operator is modifying the output.

SELECT VendorID, EmployeeID, COUNT(PurchaseOrderID)
FROM Purchasing.PurchaseOrderHeader
GROUP BY VendorID, EmployeeID

UNPIVOT does the opposite. If you have a source of data that has been pivoted, UNPIVOT will reverse the process. Here's the BOL example:
--Create the table and insert values as portrayed in the previous example.
CREATE TABLE pvt (VendorID int, Emp1 int, Emp2 int,
Emp3 int, Emp4 int, Emp5 int)
GO
INSERT INTO pvt VALUES (1,4,3,5,4,4)
INSERT INTO pvt VALUES (2,4,1,5,5,5)
INSERT INTO pvt VALUES (3,4,3,5,4,4)
INSERT INTO pvt VALUES (4,4,2,5,5,4)
INSERT INTO pvt VALUES (5,5,1,5,5,5)
GO
--Unpivot the table.
SELECT VendorID, Employee, Orders
FROM
(SELECT VendorID, Emp1, Emp2, Emp3, Emp4, Emp5
FROM pvt) p
UNPIVOT
(Orders FOR Employee IN
(Emp1, Emp2, Emp3, Emp4, Emp5)
)AS unpvt
GO
DROP TABLE pvt
GO

Here the values Emp1, Emp2...Emp5 are converted into a single column, Employee. Each of these values is store in a separate row, so 1 row becomes 5 rows.

There are 10 types of people in the world, those that understand binary, and those that don't.
Go to Top of Page

DBA in the making
Aged Yak Warrior

638 Posts

Posted - 2010-04-07 : 07:29:27
quote:
Originally posted by Delinda

Let's say, i've as follow,
....
How to use the SQL using PIVOT, and SQL using UNPIVOT based on result set above?



Firstly, You have a where clause in there, so your sample data really should supply samples either side of the where clause. Secondly, what do you expect the output format to look like?

There are 10 types of people in the world, those that understand binary, and those that don't.
Go to Top of Page

Delinda
Constraint Violating Yak Guru

315 Posts

Posted - 2010-04-07 : 08:11:46
oh. i will try by myself. tq sir
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-04-08 : 05:22:55
For dynamic PIVOT, use
http://beyondrelational.com/blogs/madhivanan/archive/2008/08/27/dynamic-pivot-in-sql-server-2005.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Delinda
Constraint Violating Yak Guru

315 Posts

Posted - 2010-04-20 : 22:35:04
tq to all
Go to Top of Page
   

- Advertisement -