This is quite a popular question and I have never wrote about this on my blog. A Pivot Table can automatically sort, count, and total the data stored in one table or spreadsheet and create a second table displaying the summarized data. The PIVOT operator turns the values of a specified column into column names, effectively rotating a table.
USE AdventureWorks
GO
SELECT [CA], [AZ], [TX]
FROM
(
SELECT sp.StateProvinceCode
FROM Person.Address a
INNER JOIN Person.StateProvince sp
ON a.StateProvinceID = sp.StateProvinceID
) p
PIVOT
(
COUNT (StateProvinceCode)
FOR StateProvinceCode
IN ([CA], [AZ], [TX])
) AS pvt;

Reference : Pinal Dave (http://blog.SQLAuthority.com)
SQL SERVER – PIVOT Table Example
May 22, 2008 by pinaldave
Posted in Pinal Dave, SQL, SQL Authority, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology | Tagged PIVOT, UNPIVOT | 21 Comments
21 Responses
Leave a Reply
-
About Pinal Dave
Pinalkumar Dave is Microsoft SQL Server MVP, Solid Quality Mentor and a prominent author of over 1000 SQL Server articles at SQLAuthority. He is a dynamic and proficient Principal Database Architect, Corporate Trainer and Project Manager specializing in SQL Server Programming with over 7 years of hands-on experience. He holds a degree in Masters of Science and has accomplished a number of certifications including MCDBA and MCAD (.NET). He has also been awarded Regional Mentor for PASS Asia.
-
Blog Stats
- 8,544,742 Readers
-
SQLAuthority Links

My Homepage
My Resume
My Other Blog
--------------------
Top Downloads
PDF Downloads
Script Downloads
Script Bank
Favorite Scripts
All Scripts - 1
All Scripts - 2
Top Articles
Best Articles
Favorite Articles - 1
Favorite Articles - 2
--------------------
SQL Interview Q & A
SQL Coding Standards
SQL FAQ Download
--------------------
Jobs @ SQLAuthority
Categories
- About Me (50)
- Best Practices (81)
- Business Intelligence (6)
- Data Warehousing (26)
- Database (258)
- DBA (121)
- DigiCorp (7)
- MVP (82)
- Poll (5)
- Readers Contribution (22)
- Readers Question (29)
- Software Development (62)
- SQL Add-On (87)
- SQL Backup and Restore (51)
- SQL BOL (8)
- SQL Coding Standards (20)
- SQL Constraint and Keys (49)
- SQL Cursor (29)
- SQL Data Storage (39)
- SQL DateTime (36)
- SQL Documentation (193)
- SQL Download (197)
- SQL Error Messages (117)
- SQL Function (106)
- SQL Humor (22)
- SQL Index (81)
- SQL Interview Questions and Answers (53)
- SQL Joins (60)
- SQL Optimization (51)
- SQL Performance (203)
- SQL Puzzle (18)
- SQL Security (114)
- SQL Server DBCC (41)
- SQL Server Management Studio (17)
- SQL Stored Procedure (96)
- SQL String (17)
- SQL System Table (28)
- SQL Trigger (26)
- SQL User Group (41)
- SQL Utility (116)
- SQL White Papers (8)
- SQLAuthority (309)
- SQLAuthority Author Visit (62)
- SQLAuthority Book Review (18)
- SQLAuthority News (280)
- SQLAuthority Website Review (23)
- SQLServer (45)
- Tech (784)
- Pinal Dave (774)
- SQL Scripts (492)
- Technology (1031)
- SQL (1031)
- SQL Authority (1031)
- SQL Query (1031)
- SQL Server (1031)
- SQL Tips and Tricks (1031)
- T SQL (1031)
-
Top Posts
- SQL SERVER - SELECT 1 vs SELECT * - An Interesting Observation
- SQL SERVER - Insert Data From One Table to Another Table - INSERT INTO SELECT - SELECT INTO TABLE
- SQL SERVER - Insert Multiple Records Using One Insert Statement - Use of UNION ALL
- SQL SERVER - Retrieve Current Date Time in SQL Server CURRENT_TIMESTAMP, GETDATE(), {fn NOW()}
- SQL SERVER - Import CSV File Into SQL Server Using Bulk Insert - Load Comma Delimited File Into SQL Server
- SQL SERVER - Convert Text to Numbers (Integer) - CAST and CONVERT
- SQL Server Interview Questions and Answers Complete List Download
- SQL SERVER - 2005 - Create Script to Copy Database Schema and All The Objects - Stored Procedure, Functions, Triggers, Tables, Views, Constraints and All Other Database Objects
- SQL SERVER - 2008 - Interview Questions and Answers Complete List Download
- SQL SERVER - Restore Database Backup using SQL Script (T-SQL)
- SQL SERVER - 2005 List All Tables of Database
- SQL SERVER - Shrinking Truncate Log File - Log Full
-
Authors
-
pinaldave
- SQL SERVER – Two Methods to Retrieve List of Primary Keys and Foreign Keys of Database
- SQL SERVER – Four Different Ways to Find Recovery Model for Database
- SQL SERVER – Restore Sequence and Understanding NORECOVERY and RECOVERY
- SQL SERVER – Backup Timeline and Understanding of Database Restore Process in Full Recovery Model
- SQL SERVER – BLOB – Pointer to Image, Image in Database, FILESTREAM Storage
- SQLAuthority News – Big Thinkers – Robert Cain
- SQL SERVER – Standby Servers and Types of Standby Servers
- SQLAuthority News – Request SQLAuthority.com Stickers and SQL Server Cheat Sheet
- SQLAuthority News – Authors Visit – K-MUG TechEd Trivandrum on June 27, 2009
- SQLAuthority News – Book Review – Murach’s SQL Server 2008 for Developers
-
Archives
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
Pages
Category Cloud
Best Practices Database DBA MVP Pinal Dave Software Development SQL SQL Add-On SQL Authority SQLAuthority Author Visit SQLAuthority News SQL Backup and Restore SQL Documentation SQL Download SQL Error Messages SQL Function SQL Index SQL Interview Questions and Answers SQL Joins SQL Optimization SQL Performance SQL Query SQL Scripts SQL Security SQL Server SQL Stored Procedure SQL Tips and Tricks SQL Utility Technology T SQL



Nice, but we have too many cases with PIVOT, can you explain in general some 4-5 situation ( don’t recommend Books Online plz everyone we know about it)!
the table example:
IDj
YEAR
OFFICE
ITEMS
PRICE
QTY
COMMENTS
and now the data should look like this:
OFFICE = ROWS
ITEMS = COLUMNS
SUM(QTY) = DETAILS
and
GROUPING BY YEAR OR FILTERING BY YEAR
thnx!
[...] 29, 2008 by pinaldave My previous article SQL SERVER – PIVOT Table Example encouraged few of my readers to ask me question about UNPIVOT table. UNPIVOT table is reverse of [...]
hi Pinal I understood the meaning of pivot query
but if i have around 100 of departments and i want to use pivot query for this to find out the avg sal departmentwise. so what to to for this ?
Is it possible to use two aggreation in the PIVOT? If possible, can you please suggestion how to use it?
e.g
SUM(Price), Count(Product)
FOR ProductId IN ( [1],[2],[3] )
Thank and regards,
Robin.
Forgot one thing that. I did by using JOIN statement. It is working but I would like to know better way.
Thank for your help.
Regards,
Robin
[...] SERVER – PIVOT and UNPIVOT Table Examples SQL SERVER – PIVOT Table Example SQL SERVER – UNPIVOT Table [...]
How to PIVOT table with date column?
Source table:
==========
SiteNo SubNo MV Date
—— ——– — ————
001 SUB-028 V1 2007-03-11
001 SUB-028 V2 2007-05-21
001 SUB-028 V3 2007-06-22
001 SUB-028 V4 2007-07-11
001 SUB-028 V5 2007-08-05
001 SUB-028 V6 2007-09-28
002 SUB-010 V1 2007-04-22
002 SUB-010 V2 2007-05-22
002 SUB-010 V3 2007-06-05
Result table:
===========
SiteNo SubNo [V1] [V2] [V3] [V4] [V5] [V6]
—— ——— ———– ——— ———- ———– ———– ———-
001 SUB-028 2007-03-11 2007-05-21 2007-06-22 2007-07-11 2007-08-05 2007-09-28
002 SUB-010 2007-04-22 2007-05-22 2007-06-05
@Aaron.
It took nearly one and half hours for me to write these 3 lines script. The main hurdle came at usage of aggregate functions used in Pivots.
Because I have to use a function with Pivot, there was no way I can display date values in V1. V2 …. V6 columns.
I have used Max function. Since you have only one date for one combination of V1 and SiteNo, this will work fine in your case, but in general this could be misleading…. as we are asking database engine to pick up max values … anyways here is your query.
SELECT SiteNo, subno ,[V1] ,[V2] ,[V3] ,[V4] ,[V5] ,[V6]
FROM (
SELECT SiteNo, subno, MV, Date
FROM SourceTable) up
PIVOT ( max(Date) FOR MV in (V1 ,V2 ,V3 ,V4 ,V5 ,V6 ) )AS pvt
ORDER BY SiteNo
I have tested Output for this script , it does matches with your expected Output.
Regards,
IM
Hi *Imran Mohammed*, it’s so great you worked it right. I was struggling on what function should be used too. Yes, in the case I posted, it works perfectly.
As you already see that actually I do have case like below, which for one MV value there’re more than one dates. I wonder if this can be done by PIVOT. If not, any other option?
SiteNo SubNo MV Date
—— ——– — ————
001 SUB-028 V1 2007-03-11
001 SUB-028 V1 2007-03-15
001 SUB-028 V1 2007-03-19
001 SUB-028 V2 2007-05-21
001 SUB-028 V3 2007-06-22
001 SUB-028 V4 2007-07-11
001 SUB-028 V4 2007-07-17
001 SUB-028 V4 2007-07-21
001 SUB-028 V4 2007-07-29
001 SUB-028 V5 2007-08-05
001 SUB-028 V6 2007-09-28
002 SUB-010 V1 2007-04-22
002 SUB-010 V2 2007-05-22
002 SUB-010 V3 2007-06-05
002 SUB-010 V3 2007-07-05
(continue last post)
The challenge is that MV value could be any string, could be V1 ~ Vx. And for Same MV value, they just want to list all dates, like
SiteNo SubNo [V1] [V1] [V1] [V2] [V3] [V4] [V5] [V6]
—— ——— ———– ——— ———- ———– ———– ———- ———— —————-
001 SUB-028 2007-03-11 2007-03-15 2007-03-19 2007-05-21 2007-06-22 2007-07-11 2007-08-05 2007-09-28
[...] Reference : Pinal Dave (http://blog.SQLAuthority.com), SQL SERVER – UNPIVOT Table Example, SQL SERVER – PIVOT Table Example [...]
Hi PLS HELP ME, i have a table like that
d a b c a1 b2 c3
401 0.3 0.8 0.9 z x y
402 0.4 0.5 0.6 x z y
And i need this result in my query
d z x y
401 0.3 0.8 0.9
402 0.5 0.4 0.6
I was thinking on pivot tables but i dont know how to do it with more colums
@xomaly i do not understand your question. So, i guessed. How about this:
WITH
Data
AS
(
SELECT 401 d, 0.3 a, 0.8 b, 0.9 c, ‘z’ a1, ‘x’ b2, ‘y’ c3 UNION ALL
SELECT 402 d, 0.4 a, 0.5 b, 0.6 c, ‘x’ a1, ‘z’ b2, ‘y’ c3
)
SELECT
d,
CASE a1
WHEN ‘z’ THEN a
WHEN ‘X’ THEN b
WHEN ‘y’ THEN c
END,
CASE b2
WHEN ‘z’ THEN a
WHEN ‘X’ THEN b
WHEN ‘y’ THEN c
END,
CASE c3
WHEN ‘z’ THEN a
WHEN ‘X’ THEN b
WHEN ‘y’ THEN c
END
FROM
Data;
THANK YOU
YOU FINALLY SOLVE MY DOUBTS
THANK YOU VERY MUCH
hi..i ve written one query it return the following results
date [name]
06:00 aaaa
06:00 bbbb
06:00 ccccc
06:30 dddd
06:30 eeee
06:30 ffffffff
06:30 gggg
07:00 hhhh
08:00 NULL
08:30 NULL
09:00 NULL
09:30 IIIIII
10:00 NULL
.
.
..
i want to display this in the following format
date name
06:00 aaaa bbbb cccc NULL
06:30 dddd eeee ffffff gggg
07:00 hhhh NULL NULL NULL
08:30 NULL NULL NULL NULL
09:00 NULL NULL NULL NULL
10:00 IIIIII NULL NULL NULL
.
.
.
.
is it possible using pivot? kindly help me
Hi Pinal,
You’re examples on pivot and unpivot are very good. However I have some questions which may or may not have to use this. Please suggest with your experience which is suitable for me.
I have a TABLE in oracle as
Australia Entry1 Apple $ 2
Japan Entry2 Guava $2
Australia Entry2 Guava $2
Hungary Entry3 Guava $2
Australia Entry2 Apple $2
Japan Entry1 Guava $2
Australia Entry4 Orange $2
Japan Entry2 Orange $2
My Output should be as follows where Amount is added w.r.t Entries as well as Fruits but in columns.
Country Entry1 Entry2 Entry3 Entry4 Apple Guava Orange
Australias $2 $4 0 $2 $4 $2 $2
Japan $2 $4 0 0 0 $4 $2
Hungary 0 0 $2 0 0 $2 0
I would like the output as above. However I do not want to use decode, which is what I am using right now.
Is it possible to use this for a varible number of columns. Basically the number of columns will not be more than 10 for sure, but I need them to be variable which is not possible by using decode.
Thanks for all your help.
Sreedhar
THis is related to above question. I can move the table to sql if needed. If I know I can get it easier from there.
THanks,
Sreedhar
Hi…
I have a table : T1
No ID Date Value
___________________________________
1 1001 01/05/2009 101.00
1 1001 15/05/2009 102.00
1 1001 20/05/2009 105.00
2 1001 01/05/2009 41.00
2 1001 15/05/2009 44.00
3 1001 01/06/2009 330.00
I need the Data in this Format
No ID 01/05/2009 15/05/2009 20/05/2009
01/06/2009
____________________________________________________
1 1001 101.00 102.00 105.00 NULL
2 1001 41.00 44.00 NULL NULL
3 1001 NULL NULL NULL 330.00
can you give me the solution for this type…
Thanks in advance
@Mahdestiny
This is the script.
– Create Table
create table T1 ( [No] int, [ID] int, Date Datetime, Value decimal (38,2))
– Insert data into the table
insert into T1 values (1, 1001, ‘05/01/2009′, 101.00)
insert into T1 values (1, 1001, ‘05/15/2009′, 102.00)
insert into T1 values (1, 1001, ‘05/20/2009′, 105.00)
insert into T1 values (2, 1001, ‘05/01/2009′, 41.00)
insert into T1 values (2, 1001, ‘05/15/2009′, 44.00)
insert into T1 values (3, 1001, ‘06/01/2009′, 330.00)
– Check the Data
select * from T1
– Using Pivot key word to get required output
SELECT [No] , [ID] ,[05/01/2009] ,[05/15/2009] ,[05/20/2009] ,[06/01/2009]
FROM (
SELECT [No], [ID], [Date], [Value]
FROM T1) up
PIVOT ( sum([Value]) FOR [Date] in ([05/01/2009] ,[05/15/2009] ,[05/20/2009] ,[06/01/2009]) )AS pvt
~ IM.
Here your using
“SELECT [No] , [ID] ,[05/01/2009] ,[05/15/2009] ,[05/20/2009] ,[06/01/2009]
FROM ( ”
i cant say how many dates will be coming from select statement.
coz im using Select * from T1, this will return me many Date.
Also im not using sum([Value]) FOR [Date] wise.
I need to show dynamically date as column and value as row using pivot.
@Mahdestiny,
Then you need to write Dynamic SQL.
First prepare the select statement with Dynamic SQL.
Once you prepare the select statement, then execute that statement.
~ IM.