Tải bản đầy đủ - 0 (trang)
9-13. PIVOTing Data in T-SQL

9-13. PIVOTing Data in T-SQL

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

Chapter 9 ■ Data Transformation



,L.SalePrice

FROM

dbo.Stock S

INNER JOIN dbo.Invoice_Lines L

ON S.ID = L.StockID

INNER JOIN dbo.Invoice I

ON L.InvoiceID = I.ID

) AS SRC

PIVOT

(

SUM(SalePrice)

FOR SaleMonth IN ([January], [February], [March], [April], [May], [June], [July],

[August], [September], [October], [November], [December])

) AS PVT

;



How It Works

Sometimes source data, even perfectly normalized source data, just does not fit the requirements of an import

process. While this is more particularly the case when creating reporting systems, it can also occur with more

traditional data import requirements. So it helps to be able to pivot source data—and transpose the data from

rows into columns. Of course, this is a data denormalization, and worse, it will cause data to be aggregated to

a point where it will become impossible to reconstitute the source. However, as long as you are aware of the

limitations and risks, it is a valid and useful technique in certain circumstances.

Before proceeding with a more detailed explanation of the code, it is probably worth clarifying the

underlying concepts and vocabulary.



Data Type



Comments



Pass-through



Data that is not affected by the transposition and that flows through the process as left-hand

column(s) without being modified in any way—or without modifying the data.



Key or Set Key



Grouping data that appears as left-hand column(s).



Pivot Column



Data from a column that will be transposed into column titles.



Pivot Data



Data from the pivot column that will be transposed and aggregated into the relevant pivot

column and row intersection.



In this example, the inner SELECT statement returns the three columns that will be used to transpose the data:

A Key column, which will be the left-hand column (Marque in this example). There can

be as many of these as you wish.

The pivot column (the SaleMonth derived from the InvoiceDate column).

The pivot data (or value column) that is aggregated and transposed (SalePrice).

This inline query—that is aliased as SRC—is then PIVOTed. This means specifying:





Which column is aggregated (SalePrice) and which aggregation function is applied—SUM

in this example.







How the column that will be transposed is extrapolated into multiple columns. This

means specifying the column names.



506

www.it-ebooks.info



Chapter 9 ■ Data Transformation



Finally, the outer SELECT specifies the pass-through columns (taken from the inline query) and the

transposed columns (taken from the PIVOT statement) that will be returned.

Inevitably, this is a fairly rigid approach since the pivoted columns have to be specified unequivocally for

the statement to work. It is also possible if you really wish (and was the approach that was used in the dark days

before SQL Server 2005) to use multiple CASE statements to transpose columns. I will leave this approach to the

history books. Most examples of pivoted data show the transposed columns as dates (years, years, and months,

etc.). This is in no way compulsory, and any source data column can be pivoted.

It is also possible to make the SQL dynamic, and so to allow for variable input data—up to a point. This

approach boils down to determining the list of pivoted columns, as a variable, and then using this variable inside

the T-SQL that pivots the data—itself defined as a T-SQL variable.

If we take the preceding example, the code for dynamic T-SQ pivoting is

(C:\SQL2012DIRecipes\CH09\DynamicPivot.sql):

DECLARE @ColumnList VARCHAR(8000)

SELECT

@ColumnList = LEFT(TR.ColList, LEN(TR.ColList)-1)

FROM

(

SELECT QUOTENAME(DATENAME(month, InvoiceDate)) + ',' AS [text()]

FROM

dbo.Invoice

GROUP BY DATENAME(month, InvoiceDate), MONTH(InvoiceDate)

ORDER BY MONTH(InvoiceDate)

FOR XML PATH('')

) TR (ColList);



DECLARE @TransposeSQL VARCHAR(MAX) = '

SELECT

Marque AS TotalMonthlySalesPerMarque, '+ @ColumnList + '

FROM

(

SELECT

S.Marque

,DATENAME(month, I.InvoiceDate) AS SaleMonth

,L.SalePrice

FROM

dbo.Stock S

INNER JOIN dbo.Invoice_Lines L

ON S.ID = L.StockID

INNER JOIN dbo.Invoice I

ON L.InvoiceID = I.ID

) AS SRC

PIVOT

(

SUM(SalePrice)

FOR SaleMonth IN ('+ @ColumnList + ')

) AS PVT

;'

EXECUTE CarSales.dbo.sp_executesql @statement = @TransposeSQL;



507

www.it-ebooks.info



Chapter 9 ■ Data Transformation



Hints, Tips, and Traps





Though not inelegant, this approach is not without its problems. Specifically, when

outputting the data, you need to be able to analyze the metadata of the destination

table, and add new columns if they do not exist when compared to the list of transposed

columns in the PIVOT statement. While not difficult, this is a little laborious, and I will not

be showing how to do it here.



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

Problem

You need to pivot data as part of an SSIS data flow.



Solution

Use the new, simplified SSIS 2012 pivot task, as follows.

1.



Create a new SSIS Package and add an OLEDB connection manager named

CarSales_OLEDB.



2.



Add an OLEDB Source component, and configure as follows:

OLEDB Connection Manager:



CarSales_OLEDB



Data Access Mode:



SQLCommand



SQL Command Text:



SELECT S.Marque

,DATENAME(month, I.InvoiceDate) AS SaleMonth

,L.SalePrice

FROM

dbo.Stock S

INNER JOIN dbo.Invoice_Lines L

ON S.ID = L.StockID

INNER JOIN dbo.Invoice I

ON L.InvoiceID = I.ID

ORDER BY S.Marque, MONTH( I.InvoiceDate);



3.



Confirm with OK.



4.



Add a Pivot task and connect the OLEDB source component to it. Double-click to

edit.



5.



Click the Pivot Key pop-up list. Select the column that contains the pivot key (or pivot

column if you prefer) data. This will become the new column title taken from a source

column of data. In this example it is SaleMonth.



6.



Click the Set Key pop-up list and select the column that contains the data that will be

grouped into the leftmost column of the pivot table. In this example, it is Marque.



7.



Click the Pivot Value pop-up list and select the column whose data will be aggregated

for each row in the pivot table.



508

www.it-ebooks.info



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



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

9-13. PIVOTing Data in T-SQL

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

×