T-SQL Pivot

To a large extent business users want to visualize data in a tabular format as shown below. This is a good presentation however, it takes 3 rows just to present data for CustomerID 11000 and 31406 rows to present data for all the CustomerIDs.

What if your requirement is to represent each year as an individual column so that in a single row, you can view the Total per CustomerID for each OrderYear? Pivot is a T-SQL operator that can be use used to achieve this. Pivot basically flips rows into columns. Let’s look at the the example below. In this case, all the data for CustomerID 11000 is presented on a single row. It takes 19119 rows to present all the data for all the CustomerIDs.

q2

In a nutshell, Pivot allows to summarize data by rotating rows into columns.

Thanks for reading and keep loving SQL 🙂

One thought on “T-SQL Pivot

Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: