You are on page 1of 13

How to Create a Pivot Table in

Six Steps
Today I want to share with you how to create a pivot or cross tab chart.
I’ll show you the steps using an example from the adventure works
database.
I get so many questions on how to create these.
Once you get the steps down, you’ll see they are not hard at all!
I’ve seen where people get hung up in many ways, such as not knowing
how to visualize the data, or write the SQL syntax.
By following my steps, we’ll overcome these hurdles and achieve
success.
I want you to work along with me on this one, so think of a pivot table
you want to create, as we’ll do it together.
So, let’s get started.

If you don’t have a pivot table in mind, how creating one from the
AdventureWorks database? Here are some ideas:
• Using the Person.Person table, count the number of people by
PersonType and EmailPromotion.
• Using the HumanResouces.Employee table, count the number of
employees by MaritalStatus and Gender.

1
© essentialSQL LLC, ALL RIGHTS RESERVED
Step 1 – State our Goal
I find that when I’m trying to solve a complicated problem, it really helps
for me to write down on paper what is I’m trying to solve.
It starts to solidify my ideas.
Here is what I’m aiming to do:

I want to summarize the number of parts located in specific


plant location by production line.

What are you trying to summarize? Write down what you’re trying to
create, getting your idea on paper helps:

When writing your goal, try to write it using the same nouns and
verbs as your database. What I mean is if you your database calls
workers employees, then don’t refer to them as associates.

2
© essentialSQL LLC, ALL RIGHTS RESERVED
Step 2 – Sketch
By sketching out the end goal you’ll see it is much easier for you to
understand the various components of a pivot chart, and as we talk
about how they are created, you be in a better position to put one
together.
Keep in mind a pivot table has three main pieces: Rows, Columns, and
Summary Values.
Our goal is to identify what data we’ll use to create these pieces.
To help me get started I sketched out what I’m aiming to produce:

My rows are Locations, the Columns are product lines, and the cells, the
quantity of parts at a location per product line.

3
© essentialSQL LLC, ALL RIGHTS RESERVED
Let’s work on your pivot table. If you want, you an either draw it on
paper like I did, our use this table to help you get your ideas down:

As you are putting your table together, think about what columns you’ll
be using.

My row value is from column __________________________________

My column value is from column _______________________________

The value I’m summarizing is from column ________________________

4
© essentialSQL LLC, ALL RIGHTS RESERVED
Step 3 – Identify Data Sources
Now let’s understand where your data is located. Don’t worry about
summarizing the data, let’s just get it pulled together.
This step is important, since in many cases the data you’ll need comes
from multiple tables, and you’ll need use joins to combine in into a
single result.
We’re doing this now, since it is far easier to build and verify the join
before we start the pivot – baby steps!

Based on the table I created in step 2, I know I’ll need locations, product
lines, and inventory quantities. I did some research and found these
columns in the following tables:
• Production.Location
• Production.Product
• Production.ProductInventory
After looking at Production.ProductInventory I realized it is central to my
query and contains foreign keys to the Location and Product tables.

5
© essentialSQL LLC, ALL RIGHTS RESERVED
So now look at the table you created. Can you identify which tables are
used to obtain the row, column, and calculated values?

If all the values come from the same table, you’re in luck, and you can go
on to the next step.
Otherwise, can you see how the data is related? Chance are, the value
you wish to calculate is “central” to your query, and the other values
related to this table.
If it helps, draw out the table relationships.

6
© essentialSQL LLC, ALL RIGHTS RESERVED
Step 4 – Write Query
Now let’s write a query to simply list the data we’ll used to construct our
pivot table.
The goal of this step is to make sure we are pulling the correct data and
that we don’t have issues, such as returning duplicate rows.
For my query I’ll return the LocationName, ProductLine, and Quantity.
Here is what my query looks like:
SELECT L.Name AS LocationName,
I.Quantity,
COALESCE(P.ProductLine, '-') AS ProductLine
FROM Production.ProductInventory I
INNER JOIN Production.Location L ON L.LocationID = I.LocationID
INNER JOIN Production.Product P ON I.ProductID = P.ProductID;

Here are the results

7
© essentialSQL LLC, ALL RIGHTS RESERVED
Again, the goal here is to just get the data we’ll use to create the pivot
table, we’re not worried about the calculation or summarization.
How about you write out your query. Be sure to test it.
Before we’re summarizing the data, and “hiding” rows, let’s make sure
we have good data.

OK, so if you’re not familiar with joins then I would recommend you
read our article Introduction to Database Joins.

8
© essentialSQL LLC, ALL RIGHTS RESERVED
Step 5 – Select Pivot Columns
Now we are ready to the final step, to create our pivot table!
Now that all the prep work is out of the way, this step becomes simple.
We’ll now “wrap” the query we built in the prior step with some pivot
table related clauses.
The first thing we’ll do is create a select list of column name for the row
values, and the exact column header values.
For my example this is LocationName for the row values, and the various
product lines values, M, R, S, and T.
SELECT LocationName, [M], [R], [S], [T], [-]

Now we’ll select this from our original query:


SELECT LocationName, [M], [R], [S], [T], [-]
FROM
(
SELECT COALESCE(P.ProductLine, '-') AS ProductLine,
I.Quantity,
L.Name AS LocationName
FROM Production.ProductInventory I
INNER JOIN Production.Location L ON L.LocationID = I.LocationID
INNER JOIN Production.Product P ON I.ProductID = P.ProductID
) AS PivotData

If you’re having troubles knowing what the columns should be, then
take your original query, and select the DISTINCT value.
Like this!
SELECT DISTINCT COALESCE(P.ProductLine, '-') AS ProductLine
FROM Production.ProductInventory I
INNER JOIN Production.Location L ON L.LocationID = I.LocationID
INNER JOIN Production.Product P ON I.ProductID = P.ProductID;

The results are the columns you’ll want to list.

9
© essentialSQL LLC, ALL RIGHTS RESERVED
Step 6 – Add PIVOT clause
The last thing to do is to add the PIVOT clause.
The PIVOT clause instructs the query what to summarize, and by which
rows and columns.
Here is the pivot CLAUSE for my query:

Notice that I’m getting the SUM of the quantity. Also check out the FOR
clause. There you’ll see that we’re summarizing columns by ProductLine
for specific ProductLine values.
With the PIVOT clause understood, all that remains is to add it to the
bottom or our current query.
Here we’ll add the final touches to our query to pivot the data.
SELECT LocationName, [M], [R], [S], [T], [-]
FROM
(
SELECT COALESCE(P.ProductLine, '-') AS ProductLine,
I.Quantity,
L.Name AS LocationName
FROM Production.ProductInventory I
INNER JOIN Production.Location L ON L.LocationID = I.LocationID
INNER JOIN Production.Product P ON I.ProductID = P.ProductID
) AS PivotData
PIVOT(SUM(Quantity)
FOR ProductLine IN([M], [R], [S], [T], [-])) AS PivotResult
ORDER BY LocationName;

10
© essentialSQL LLC, ALL RIGHTS RESERVED
Here is the same query with the various pieces highlighted, so you can
see how the parts fit together…

11
© essentialSQL LLC, ALL RIGHTS RESERVED
Let’s finalize your query. First write the clause to select the pivot
columns. You learned about this in step 5.

Place Pivot Clause from Step 5 here

FROM (

Place query from Step 4 here

) as PivotData

Place PIVOT clause from Step 6 here

12
© essentialSQL LLC, ALL RIGHTS RESERVED
Resources
There quite a bit that goes into a PIVOT table. To help you get the most
out of this guide, I put together some resources you may want to use to
help with some of the “foundational” challenges, such as knowing how
to navigate your database to find table relationships, and so on.

Each of these resources is a good starting point to learn more!


Learn the Three Crucial Steps to Write Better SQL
Introduction to Database Joins
Derived Tables – Subquery in FROM clause
What is a Dynamic Pivot Table?

Also, I would encourage you to join my essential SQL learning group and
join my active community of aspiring business analysts and accidental
DBAs!

Have a great day – Kris.

13
© essentialSQL LLC, ALL RIGHTS RESERVED

You might also like