SQL Server Performance

  • Home
  • Articles
  • Forums
  • Tips
  • Training
  • FAQ's
  • Blogs
  • Software
  • Books
  • About Us
RSS Feeds
Sign in | Join


Article Topics

All Articles
Performance Tuning
Audit
Business Intelligence
Clustering
Reporting Services
Developer
General DBA
ASP.NET / ADO.NET

Training Videos

Check out our new SQL Server Training Videos section More...

Write for Us

Share your SQL Server knowledge with others and raise your profile in the community More...
Latest Articles

Writing Custom Rules for FxCop
Tuning SQL Server 2005 Integration Services for EBCDIC Data Conversion ...
Using The Transfer Jobs Task in SQL Server Integration Services ...
Using Transfer Error Messages and Transfer Master Stored Procedures Tasks in ...

More     
 
Latest FAQ's

XQuery [query()]: Attribute may not appear outside of an element ...
Cannot find either column "%.*ls" or the user-defined function or aggregate ...
%sThe operator "%ls" cannot be applied to "%ls" and "%ls" operands. ...
%sOnly 'http://www.w3.org/2001/XMLSchema#boolean?' or 'node()*' expressions allowed in conditions and with logical ...

More     
   
Latest Software Reviews

Spotlight on ApexSQL Diff - Server-based database comparison tool ...
Spotlight on ApexSQL Data Diff - Server-based database comparison tool ...
Spotlight on ApexSQL Doc 2008
ApexSQL Enforce

More     

articles >> performance tuning >> Pivot and UnPivot with SSIS

Pivot and UnPivot with SSIS

By : Dinesh Asanka
Nov 28, 2007

Introduction

The presentation of your data is required for easy analysis. Turning columns into rows and rows into columns is another way of presenting your data so that end-users can understand it easily. Other than presentation purposes, you need to transform your data within your data warehouse application into different data formats. This process can be referred to as ‘Pivoting’, and the reversing of this process can be referred to as ‘UnPivoting’.

In this article, we will discuss how you can use the Pivot and Unpivot processes in SQL Server Integration Service (SSIS).


Requirements

As this article will not tell you how to write SSIS packages, it is a requirement to have a fair amount of experience in creating SSIS packages. However, I will try to discuss details of creating SSIS packages wherever necessary without disturbing the main topics of the article. In addition, it is necessary to know how to write a query with joining three or more tables.

Software requirements include SQL Server 2005 installed with SQL Server Business Intelligence Development Studio. This article is written with the assumption that the Adventureworks database’s data is being used. It would be optimal for the reader to have an installed Adventureworks database along with a SQL Server 2005 database server.

The data file for this exercise can be found here .

Pivot

To understand what pivoting is, let’s see an example. Below illustrates the relationship in the Advenureworks database of Sales.SalesOrderHeader, Sales.SalesOrderOrder, Prodcution.Product and Production.ProductCategory tables.

From the above relationship, we can assume that we need following output.

Product

Qrt1

Qtr2

Qrt3

Qrt4

Accessories

 

 

870

411

Bikes

1167

1369

2844

2495

Components

2641

2966

6173

5253

It is known that you will not retrieve the above data set by a simple T-SQL query. However, we can build the following format with a simple T-SQL code.

Name

Qtr

OrderQty

Accessories

3

870

Accessories

4

411

Bikes

1

1167

Bikes

2

1369

Bikes

3

2844

Bikes

4

2495

Components

1

2641

Components

2

2966

Components

3

6173

Components

4

5253

Turning the above table into the required format is an example of “pivoting”.

Let’s start with the design package. First, add a new package after creating Integration Project.

Then add a Data flow task to control flow. Next, add an OLE DB connection and configure that OLE DB connection to the Adventureworks database.

Finally, add an OLE DB source to the added data flow. While we are going to design the SSIS package with minimum coding, we will use as many SSIS controls as possible. Thus, we will add following T-SQL to the added OLE DB Source.

SELECT PC.Name

,soh.OrderDate

,SOD.OrderQty

FROM Sales.SalesOrderDetail SOD

INNER

JOIN Sales.SalesOrderHeader SOH

ON SOH.SalesOrderID = SOD.SalesOrderID

INNER

JOIN Production.Product PROD

ON Prod.ProductID = SOD.ProductID

INNER JOIN production.ProductCategory PC

ON PROD. ProductSubcategoryID = PC.ProductCategoryID

 

After including the above code, the OLE DB Source will resemble the screenshot below.


We are going to add a condition split since we are doing this pivot for only a ‘year’. To improve the usability, I have included a variable name called ‘intYear’. Therefore, if you want to change the year, it is just a matter of changing the value of the variable ‘intYear’.

The following is a screenshot of the conditional split.


In the above case, the value of @User::intYear is set to 2001.


    Next Page>>    








Home | Peformance Articles | Audit Articles | Business Intelligence Articles | Clustering Articles | Developer Articles | Reporting Services Articles | DBA Articles | ASP.NET / ADO.NET Articles | SQL Server Training Videos | DBA FAQ's | Developer Peformance FAQ's | DBA Peformance FAQ's | Developer FAQ's | Clustering FAQ's | Error Messages | Audit Tool Reviews | Backup Tool Reviews | Coding Tool Reviews | Compare Tool Reviews | Documentation Tool Reviews | Design Tool Reviews | Monitoring Tool Reviews | Log Tool Reviews | Reporting Tool Reviews | Clustering Tool Reviews | Security Tool Reviews | Change Management Tool Reviews | Remote Access Tool Reviews | Book Reviews | Security Tool Reviews | QDPMA Performance Tuning | ADO.NET / ASP.NET | Administration | Analysis/OLAP Services | Application Development | Configuration | Components | ETL | Hardware | High Availability | Hints | Index | Misc | Operating Systems | Performance Tuning | Replication | T-SQL | Views


              © 1999-2008 by T10 Media. All rights reserved