Beruflich Dokumente
Kultur Dokumente
The following diagram shows a data set after the data has been pivoted on the
Product column.
To pivot data efficiently, which means creating as few records in the output dataset as
possible, the input data must be sorted on the pivot column. If the data is not sorted,
the Pivot transformation might generate multiple records for each value in the set key,
which is the column that defines set membership. For example, if the dataset is
pivoted on a Name column but the names are not sorted, the output dataset could
have more than one row for each customer, because a pivot occurs every time that the
value in Name changes.
The input data might contain duplicate rows, which will cause the Pivot transformation
to fail. "Duplicate rows" means rows that have the same values in the set key columns
and the pivot columns. For example, if you use the data set before the data is pivoted
on the Product column, as shown in the diagram, and add a row with Kate in the Cust
column and Soda in the Product column, these duplicates values would cause the
Pivot transformation to fail, regardless of the quantity in the Qty column. To avoid
failure, you can either configure the transformation to redirect error rows to an error
output or you can pre-aggregate values to ensure there are no duplicate rows. For
example, in the sample data set, you could sum the values in the Qty column by
customer and product.
The Pivot transformation uses the properties on its input and output columns to define
the pivot operation.
The Pivot transformation includes the PivotKeyValue custom property. This property
can be updated by a property expression when the package is loaded. For more
information, see Integration Services Expression Reference, Using Property
Expressions in Packages, and Transformation Custom Properties.
This transformation has one input, one regular output, and one error output.
Configuring the Sample Dataset
The sample dataset shown in the diagram was configured as follows: the PivotUsage
property of the Cust column was set to 1, to indicate that it is a set key column; the
PivotUsage property of the Product input column was set to 2, to indicate that a
column must be created for each product; the PivotUsage property of the Qty input
column was set to 3, to indicate that quantity values are placed into the pivot column.
The transformation output was configured to include six columns. The columns, which
can be added by using the Advanced Editor dialog box, were named Cust, Ham,
Soda, Milk, Beer, and Chips. The PivotKeyValue property of the Ham column was
set to Ham, to indicate that the transformation should look for that value in the input
column. Similarly, the PivotKeyValue property of the Soda column was set to Soda,
and so on.
Columns in the transformation input were then mapped to columns in the output.
The SourceColumn property of the Cust column was configured to use the lineage
identifier of the Cust input column. The SourceColumn properties of the Ham, Soda,
Milk, Beer, and Chips columns were configured to use the lineage identifier of the
Qty input column. Another way to configure this would be to set the SourceColumn
property of the Ham, Soda, Milk, Beer, and Chips columns to -1, which would insert
the value True instead of the data value. For example, instead of the values 12 and 24,
the Beer column would then contain the value True, to indicate only that the customer
had purchased the product, instead of showing the quantity purchased.
The rows in the transformation output contain the values from the Cust and Qty input
columns.
Pivot Options
You set the PivotUsage property of the input columns to specify the role each column
performs in the pivoting process. The valid values of PivotUsage are 0, 1, 2, and 3.
The following table describes the PivotUsage options.
Option Description
The column is not pivoted, and the column values are passed through to the
0
transformation output.
The column is part of the set key that identifies one or more rows as part of
1 one set. All input rows with the same set key are combined into one output
row.
The column is a pivot column. At least one column is created from each column
2
value.
The values from this column are placed in columns that are created as a result
3
of the pivot.
Configuring the Pivot Transformation
You can set properties through SSIS Designer or programmatically.
For more information about the properties that you can set in the Advanced Editor
dialog box or programmatically, click one of the following topics:
• Common Properties
• Transformation Custom Properties
For more information about how to set the properties, click one of the following topics:
• How to: Set the Properties of a Data Flow Component in the Properties Window
• How to: Set the Properties of a Data Flow Component Using the Advanced
Editor
Unpivot Transformation
Updated: 14 April 2006
The Unpivot transformation makes an unnormalized dataset into a more normalized
version by expanding values from multiple columns in a single record into multiple
records with the same values in a single column. For example, a dataset that lists
customer names has one row for each customer, with the products and the quantity
purchased shown in columns in the row. After the Unpivot transformation normalizes
the data set, the data set contains a different row for each product that the customer
purchased.
The following diagram shows a data set before the data is unpivoted on the Product
column.
The following diagram shows a data set after it has been unpivoted on the Product
column.
Under some circumstances, the unpivot results may contain rows with unexpected
values. For example, if the sample data to unpivot shown in the diagram had null
values in all the Qty columns for Fred, then the output would include only one row for
Fred, not five. The Qty column would contain either null or zero, depending on the
column data type.
The Unpivot transformation includes the PivotKeyValue custom property. This
property can be updated by a property expression when the package is loaded. For
more information, see Integration Services Expression Reference, Using Property
Expressions in Packages, and Transformation Custom Properties.
This transformation has one input and one output. It has no error output.
Configuring the Unpivot Transformation
You can set properties through SSIS Designer or programmatically.
For more information about the properties that you can set in the Unpivot
Transformation Editor dialog box, click one of the following topics:
• Unpivot Transformation Editor
For more information about the properties that you can set in the Advanced Editor
dialog box or programmatically, click one of the following topics:
• Common Properties
• Transformation Custom Properties
For more information about how to set the properties, click one of the following topics:
• How to: Set the Properties of a Data Flow Component Using a Component
Editor
• How to: Set the Properties of a Data Flow Component in the Properties Window
• How to: Set the Properties of a Data Flow Component Using the Advanced
Editor
Pivot and UnPivot with SSIS
By : Dinesh Asanka
Nov 28, 2007
Page 2 / 5
Next, we need to derive the Quarter. Even though we can modify the initial T-SQL to
return the Quarter, I have used derive column data flow transformation task. The
following expression is used to derive the Quarter.
MONTH(OrderDate) >= 1 && MONTH(OrderDate) <= 3 ? 1 :
MONTH(OrderDate) >= 4 && MONTH(OrderDate) <= 6 ? 2 :
MONTH(OrderDate) >= 7 && MONTH(OrderDate) <= 9 ? 3 :
MONTH(OrderDate) >= 10 && MONTH(OrderDate) <= 12 ? 4 : 0
We now need to group the above data with Category and Quarter. We can use
aggregate transformation and configure it to be grouped by Name and intQtr.
Next we need to add a sort transformation, and here I have used category to sort. We
also need to sort the key column, otherwise pivot will not work properly. To see the data
up to this point,you can add a data viewer.
Below is the scrennshot of the data set should be getting, which is the data set we
need to pivot.
We have now reached the core part of this article- pivoting. For pivoting, there is a
pivot transformation confirguration which is not exactly straight forward. At input tab of
the pivot transformation, you need to select columns that you would use in the pivot
operation, which in this case would be all three available columns.
The next most important tab is the ‘Input and Output’ properties tab, pictured below.
For input columns, we need to configure the pivot usage attribute.
Optio Description
n
0 The column is not pivoted, and the column values are passed through to
the transformation output.
1 The column is part of the set key that identifies one or more rows as part
of one set. All input rows with the same set key are combined into one
output row.
2 The column is a pivot column. At least one column is created from each
column value.
3 The values from this column are placed in columns that are created as a
result of the pivot.
According to the above table, Name column should be Option 1 , intQtr should have
Option 2 and OrderQty should have Option 3 for pivot usage attribute value.
SSIS: UNPIVOT Transformation
Turning some of columns into rows was one of the tasks had to be done recently. Even though I
couldn’t use “SSIS UNPIVOT transformation” for that, I had a chance to play with it. As it is
really a useful data flow item for some operations, thought to make a post on it.
Simple create a SSIS package and add necessary source (for the text file) and destination items.
Then add a UNPIVOT transformation item and set the source output path to it. Open the
UNPIVOT transformation editor and set equipments, transportation, rental, software and
hardware as Input Column. Do not select ProjectName. Set “Amount” for all Destination Column
of all Input Columns. The Pivot Key Value will be same as Input Column name. Enter
“ExpenseType” for Pivot key value column name. Set the output of UNPIVOT transformation item
to the destination. It is done!
Since my requirement was little bit different, I had to load them to the SQL Server temp table
and use UNPIVOT TSQL command. But for scenario like this, this method can be easily applied.
You may be adding Data Conversion item to convert data if the destination is SQL Server.
How to convert a row to a column
•
○ Alert Me
○ Alert Me
0 votesVote As Helpful
Output
I thing i have to use pivot transformation in the data flow but i am not sure how to
configure this. Can anyone suggest me how to implemet this or configure this!
Thanks in advance!
Report As Abuse
○ Reply
○ Quote
○ Quote
All Replies
It appears that you would really just like to present the data differently, which will
be a front end application job (C# or VB).
Pivot:
===>
Notice that the name in the column "Type" is treated as the column name when
unpivoted. These names can be set up to be different than the value which causes
them to pivot, but most people will leave them the same so as to not confuse
themselves later.
The value that was stored in the column "Amount" is transferred to the column
name associated with it.
UnPivot:
Name typeA typeB typeC
name1,2,3,0
name2,0,1,0
name3,0,0,4
==>
Notice that the column name is transferred to the pivoting column "Type" and the
value that was stored in that column is pivoted into the column "Amount". The
record is identified by the key value of name.
<Rant>
NOTE:
There have been a few questions lately on how you would pivot multiple rows,
which usually look something like the following:
Name T1 D1 T2 D2
a,txt1,01/01/2000,txt2,01/01/2007
b,txt7,02/02/2010,txt3,08/08/2004
Name T D
a,txt1,01/01/2000
a,txt2,01/01/2007
b,txt7,02/02/2010
b,txt3,08/08/2004
Report As Abuse
○ Reply
○ Quote
○ Propose As Answer
○ Propose As Answer