Using Pivot transformation we can interchange the rows into columns.
Example:
Source table:
CREATE TABLE
PIVOT_SRC
(
YEARS INT,
PRODUCT VARCHAR(30),
TOTAL_PRICE NUMERIC(12,4)
)
INSERT INTO
PIVOT_SRC VALUES
(2010,'SOAP',3500.25)
,(2010,'PASTE',2100.61)
,(2010,'BRESH',3201.45)
,(2011,'SOAP',2842.15)
,(2011,'PASTE',2236.24)
,(2011,'BRESH',3654.75)
,(2012,'SOAP',2689.26)
,(2012,'PASTE',2432.55)
,(2012,'BRESH',3214.23)
Target table:
SSIS Package:
O/P:
CREATE TABLE
PIVOT_TARGET
(
YEARS INT,
SOAP NUMERIC(12,4),
BRESH NUMERIC(12,4),
PASTE NUMERIC(12,4)
)
SSIS Package:
- Connect to the SQL Server and create the above tables.
- Open SSIS Package, In 'Control Flow' drag and drop 'Data flow task' from Toolbox.
- Go to Data flow and drag and drop 'OLE DB Source' and configure with source table.
- From Toolbox drag and drop 'Pivot Transformation', and connect 'OLE DB Source' green arrow.
- Edit the 'Pivot Transformation'.
- Go to 'Input Columns', Select required 'Input Columns' check boxes.
- Go to 'Input and Output Properties'
- Expand the 'Pivot Default Input' and 'Pivot Default Output', Using '+' sign.
- In the above screen shot we can see the input columns YEARS,PRODUCT AND TOTAL_PRICE. For these columns we need to set the 'PivotUsage' property value based on column usage.
Pivot
Usage key |
Description
|
Sample Source
columns |
0
|
The column will passed through unaffected
|
|
1
|
The column values become the rows of
the pivot
|
YEARS
|
2
|
The column values become the
column names of the pivot
|
PRODUCT
|
3
|
The column values that are pivoted in
the pivot
|
TOTAL_PRICE
|
- Based on Source column usage, i have assigned 'PivotUsage' values. (Years-1,Product-2 and Total_Price-3)
- Click on 'Output Columns' and add New columns using 'Add Column' Button. Rename the New columns.
- After creating output columns, We need to assign 'PivotKeyValue and SourceClumn' Property values for each 'Pivot Default Output' columns. Then only mapping will create from input columns to the output columns.
- 'YEARS' output column values should come from 'YEARS' input column. So We need to assign the input column 'LineageID' values to the output column "SourceClumn property", and output column name to the output column 'PivoteKeyValue'.
- Same way we need to map remaining columns.
Output Column name
|
Mapping Input column
|
Years
|
PioveteKeyValue: Name of the Output Column (Years)
SourceColumn: LinageID of the 'YEARS' column |
Soap
|
PioveteKeyValue: Name of the Output Column (Soap)
SourceColumn: LinageID of the 'TOTAL_PRICE' column |
Paste
|
PioveteKeyValue: Name of the Output Column (Paste)
SourceColumn: LinageID of the 'TOTAL_PRICE' column |
Brush
|
PioveteKeyValue: Name of the Output Column (Brush)
SourceColumn: LinageID of the 'TOTAL_PRICE' column |
- Click on 'OK' Button.
- Drag and drop 'OLE DB Destination' and configure.
- Run the ETL.
O/P:
No comments:
Post a Comment