Pivoting Data
Pivoting data involves rotating data from a state of rows to a state of columns, possibly aggregating
values along the way. Don’t worry that this description isn’t enough to clarify exactly what pivoting
data means; this is a subject best explained through examples. In many cases, pivoting of data is
handled by the presentation layer. This section teaches you how to handle pivoting with T-SQL for
those cases that you do decide to handle in the database.
For the rest of the topics in this chapter, I use a sample table called dbo.Orders that you create and
populate in the TSQL2012 database by running the code in Listing 7-1.
LISTING 7-1
Code to Create and Populate the dbo.Orders Table
USE TSQL2012;
IF OBJECT_ID('dbo.Orders', 'U') IS NOT NULL DROP TABLE dbo.Orders;
CREATE TABLE dbo.Orders
(
orderid INT NOT NULL,
orderdate DATE NOT NULL,
empid INT NOT NULL,
custid VARCHAR(5) NOT NULL,
qty INT NOT NULL,
CONSTRAINT PK_Orders PRIMARY KEY(orderid)
);
www.it-ebooks.info
Do'stlaringiz bilan baham: |