Tải bản đầy đủ - 0 (trang)
9-14. Pivoting Data in SSIS with SQL Server 2012

9-14. Pivoting Data in SSIS with SQL Server 2012

Tải bản đầy đủ - 0trang

Chapter 9 ■ Data Transformation



8.



Click in the “Generate pivot output columns from values” field and replace

[value1],[value2],[value3] with the list of new column headers for the pivot key. These

should correspond to the distinct values in the Set Key list.



9.



Click Generate Columns Now. Confirm the dialog box that confirms the column list.

The dialog box should look like Figure 9-13.



Figure 9-13.  Pivoting data in SSIS 2012

10.



Confirm with OK.



You can now run the package and unpivot the source data.



How It Works

If you have suffered with the trickier aspects of the SSIS pivot task, then you will no doubt be relieved to know

that SQL Server 2012 makes this particular process so much easier thanks to a much enhanced user interface that

guides you through the pivot process to a large extent.



509

www.it-ebooks.info



Chapter 9 ■ Data Transformation



What is essential to understand are the following four concepts:

Pass-through elements (data that is not affected by the pivot operation).

The key or Set Key (the grouping element).

The pivot column (transposed into column titles from row elements).

The pivot data.

Once you have established that, all you have to do is to select the relevant column in the dialog box shown in

Figure 9-15.

Another way to return the list of Pivot Key values is to use SQL to generate a concatenated list of field names

using a variation on the concatenation technique described in Recipe 9-8. It could be something like this:

SELECT

LEFT(CA. CnCatLst, LEN(CA. CnCatLst)-1)

FROM

(

SELECT

'[' + DATENAME(month, I.InvoiceDate) + '],' AS [text()]

FROM

CarSales.dbo.Invoice I

GROUP BY DATENAME(month, I.InvoiceDate)

FOR XML PATH('')

) CA (CnCatLst) ;



Hints, Tips, and Traps





This new SSIS task may not be perfect, admittedly. However, it does make pivoting data in

SSIS much easier.







If you have a long list of pivoted columns, then you can obtain their names by checking the

“Ignore unmatched Pivot Key values and report them after execution” box. Then run

the package and select View/Output. The missing fields will be listed as information in the

Output window. You can copy and paste them from there into the Pivot task editor. You

must define at least one pivot column first,however. The PIVOT task will only use the SUM

function, no other aggregations (count, average, etc.) are possible.







You must SORT on the Key column(s) in the source component SQL (if importing using

SQL to define the source data) or using a Sort transform (if there is no other way of

ensuring sorted data) to ensure that all pivots for an identical key appear on the same

row. Otherwise, the results will be more than a little surprising.







If there are multiple pass-through columns, only the first value is returned—there is no

concatenation.







Duplicate records will cause failure (“duplicate rows” means rows that have the same

values in the set Key columns and the Pivot columns, only no other data is analyzed). Use

SELECT DISTINCT (if duplicates) or GROUP BY/SUM (to preaggregate) in the source SQL or

add an Aggregate transform before the PIVOT transform if you want to avoid an apparently

inexplicable (and extremely annoying) apparent failure.







The limitations described in the last two points can be overcome in many cases by

extending the SQL SELECT statement used to source the initial data.



510

www.it-ebooks.info



Chapter 9 ■ Data transformation



9-15. Pivoting Data in SSIS with SQL Server 2005 and 2008

Problem

You need to pivot data as part of an SSIS ETL process using an older version of SSIS.



Solution

Use the SSIS Pivot task. The following steps explain how to do this.

1.



As the first three steps using SQL Server 2012 are identical to those required for

previous versions, you need to carry out steps 1 to 3 from Recipe 9-16.



2.



Click the Input Columns tab and select the three input columns: Marque, SaleMonth,

and SalePrice.



3.



Click the Input and Output properties tab and expand Pivot Default Input/Input

Columns. Configure the PivotUsage properties of the input columns as follows:

Marque:



1



SaleMonth:



2



SalePrice:



3



4.



Expand Pivot Default Output/ Output Columns. Click Add Column three times to add

the following three columns: Marque, March, and May.



5.



Configure the properties of the input columns as follows:

Marque:



SourceColumn (LineageID of the source column Marque.)



March:



SourceColumn (LineageID of the source column SalePrice.)

PivotKeyValue (March)



May:



SourceColumn (LineageID of the source column SalePrice.)

PivotKeyValue (May)



6.



Confirm with OK.



How It Works

Pivoting data with SQL Server 2005 and 2008 will require some manual intervention that requires you to

understand the concepts and data. This can seem somewhat counterintuitive at first, but hopefully will become

clearer as you configure an SSIS Pivot task.

As I have suggested before, this is a fairly rigid approach because the pivoted columns have to be defined

individually and completely (with none overlooked) for the transform to work. You can anticipate future columns

when configuring the task, however. These will contain NULLs until source data becomes available.



511

www.it-ebooks.info



d



Chapter 9 ■ Data Transformation



There are four potential column types whose PivotUsage must be set for each input column:





0 = Pass-through (data that is not affected by the pivot operation)







1 = Key or Set Key (the grouping element)







2 = Pivot column (transposed into column titles from row elements)







3 = Pivot data



For each column (whatever its type among the four defined), you must specify the LineageID of the source

column and place this in the SourceColumn property. For the pivot columns only, add the source lineage ID of the

VALUE as SourceColumn, and the element from the pivot column as the PivotKeyValue. Add a column title as

the Name.



Hints, Tips, and Traps





All the comments which applied to pivoting records in SSIS 2012 in Recipe 9-14 apply

equally to older versions of SQL Server.



9-16. Consolidating Multiple Identical Data Sources in T-SQL

Problem

You need to ensure that data from more than one source flows into a unique destination using T-SQL. Your input

sources are identical in that they provide the same columns of data.



Solution

Use UNION, UNION ALL, and INTERSECT. For example, the following code snippet will output all the records from

the identical source tables (C:\SQL2012DIRecipes\CH09\ConsolidateDateSources.sql):

SELECT ClientName, County, Country FROM dbo.Client

UNION ALL

SELECT ClientName, County, Country FROM dbo.Client1;

Should you wish to output only the unique values, then use:

SELECT ClientName, County, Country FROM dbo.Client

UNION

SELECT ClientName, County, Country FROM dbo.Client1;

To return only the records having distinct values that are present and identical in both queries use:

SELECT ClientName, Country, Country FROM dbo.Client

INTERSECT

SELECT ClientName, County, Country FROM dbo.Client1;



How It Works

There will be many occasions when you will need to ensure that data from more than one source flows into a

unique destination. This will probably involve consolidating data from multiple identically structured sources

into a single destination, whose structure is also identical to the source.



512

www.it-ebooks.info



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

9-14. Pivoting Data in SSIS with SQL Server 2012

Tải bản đầy đủ ngay(0 tr)

×