B.5. PIVOTS (OR ROTATING DATA OR CROSS TABULATIONS)

Pivoting, also called rotating or cross-tabulating a table, is similar to creating a spreadsheet Pivot Table. It roughly “pivots” a table to rotate some row values into columns to show summary information. Pivoting allows you to summarize a data column by primary and secondary columns.

Primary source column heading becomes first column heading in the output.

Secondary source column values become output column headings 2 through N.

The data column of the source table is presented as summary values under output columns 2 through N. It is often summarized using SUM().

First pivot example—

Consider the following table, SalesByQtr:

SQL
										SELECT * FROM SalesByQtr;
									
 
Result
Year              Quarter              Amount ...

Get Transact-SQL Desk Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.