| 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 |
 |
|
|
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,NoOfEmployeefrom(select count(t1.EmployeeID) as NoOfEmployee, t2.Name as DepartmentNamefrom @Employees t1 inner join @Departments t2on t1.DepartmentID=t2.DepartmentIDgroup by t1.DepartmentID,t2.Name)t1 where NoOfEmployee < 10OutputDepartmentName | NoOfEmployee---------------------D1 1D2 2D3 1D4 1How to use the SQL using PIVOT, and SQL using UNPIVOT based on result set above? |
 |
|
|
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 AdventureWorksGOSELECT VendorID, [164] AS Emp1, [198] AS Emp2, [223] AS Emp3, [231] AS Emp4, [233] AS Emp5FROM (SELECT PurchaseOrderID, EmployeeID, VendorIDFROM Purchasing.PurchaseOrderHeader) pPIVOT(COUNT (PurchaseOrderID)FOR EmployeeID IN( [164], [198], [223], [231], [233] )) AS pvtORDER 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)GOINSERT 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, OrdersFROM (SELECT VendorID, Emp1, Emp2, Emp3, Emp4, Emp5 FROM pvt) pUNPIVOT (Orders FOR Employee IN (Emp1, Emp2, Emp3, Emp4, Emp5))AS unpvtGODROP TABLE pvtGO 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. |
 |
|
|
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. |
 |
|
|
Delinda
Constraint Violating Yak Guru
315 Posts |
Posted - 2010-04-07 : 08:11:46
|
| oh. i will try by myself. tq sir |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
Delinda
Constraint Violating Yak Guru
315 Posts |
Posted - 2010-04-20 : 22:35:04
|
| tq to all |
 |
|
|
|
|
|