SQL Server knowledge center

everything about SQL Server
See also: Other Geeks@INDC

Unpivot columns in SSIS

Unpivot columns in SSIS By : Kasim Wirama, MCDBA, MVP SQL Server If you see my previous article about pivoting data using SSIS component called Pivot, you will agree with me that it is easy to use, all you need to do is to define relational query that in turns will be converted to the pivot format. Now here is the vice versa condition, you are given table in pivot format, and there is a requirement that need to convert from pivot format to unpivot format. SSIS provides the component to make it possible and easy to use, the component name is Unpivot component. Some properties of the component that you must configure are : 1. input columns columns that are needed as input column stream 2. destination column a column name that you will unpivot from pivoted columns to unpivoted rows 3. pivot key value Unique name for each pivot columns, usually same with input columns. 4. pivot key value column name New column name in unpivoted form that contains caption name of pivoted columns. Let’s look pivoted form at AdventureWorks database, and you query select statement from view called [Sales].[vSalespersonsalesbyfiscalyears]. You notice that it displays sales amount for each sales person from 2002 until 2004. And final results of unpivoted as follows : SalesYear AmountSales SalesPersonID FullName Title SalesTerritory 2002 1951086.8256 275 Michael G Blythe Sales Rep Northeast 2003 4743906.8935 275 Michael G Blythe Sales Rep Northeast 2004 4557045.0459 275 Michael G Blythe Sales Rep Northeast 2002 2800029.1538 276 Linda C Mitchell Sales Rep Southwest 2003 4647225.4431 276 Linda C Mitchell Sales Rep Southwest 2004 5200475.2311 276 Linda C Mitchell Sales Rep Southwest …..so forth SalesYear and AmountSales are new columns in unpivoted format. Pivot key values are pivoted column (2002-2004). Pivot key value column name is caption name of pivot column name that is mapped to SalesYear unpivot column name. Destination columns are AmountSales. Input columns are all columns including pivoted columns. You can see how it works by starting new packages, and dragging OLEDB source component, OLEDB destination component, and Unpivot component. Connect each of them from Source – Unpivot – Destination, and specify query for the view at source component. If you open transformation editor of unpivot component, you will see 4 properties as I mention above, configure it. Configure columns of salespersonid, fullname, title, salesterritory as pass through column, the rest (pivoted columns) as input columns. Run your package, and the final results similar to what I display above. With fewer steps of configurations, you will do unpivot data extremely easily.
Share this post: | | | |

Comments

zxevil163 said:

MRzOVY Hi from Russia!

# March 17, 2008 7:32 AM

zxevil163 said:

MRzOVY Hi from Russia!

# March 17, 2008 7:33 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 
Are you human?:  


Enter the numbers above: