SqlShare.com Logo
 
Skip Navigation Links
Home
Video List
Events
Classes
About Us
Login / Register
Subscribe RSS Feed 

Takes Query Plan Analysis and Performance Monitoring to a New Level

PIVOT using the CASE statement

Pivoting data in SQL Server is a feature used mostly by reporting queries and interfaces. This video shows you how to PIVOT data using the CASE statement with T-SQL - a technique that works in all versions of SQL Server.

Duration:
3 mins 0 secs
Skill Level:
100
Rating:
4.20 out of 5
Publish Date:
November 03, 2008
PIVOT using the CASE statement You must be logged in to view this video.  
Bookmark and Share
 
1=Poor, 3=Good, 5=Excellent

About the Author

Image of Plamen Ratchev
Plamen Ratchev has over two decades experience in the software architecture and development field. He has enjoyed building solutions for UNIX, DOS and Windows platforms, with main focus on SQL Server since version 4.21. Plamen is founder of Tangra, specializing in relational database applications analysis, implementation, and tuning. His particular interest is in design patterns, performance a...

References



Comments
ian on 1/21/2009
I would like to be able to print the solution

Robert Thomson on 5/15/2009
The questions had nothing to do with the video. I think you got the wrong video. Nothing was mentioned about pivot.

Lou Gallo on 5/15/2009
Is there anyway to Transpose a table result so that what appears in a column is returned as a row? I've done this using C# in the CLR. Is there a way to do this in T-SQL?

4F2F0FFB9A on 5/15/2009
Video doesn't actually use the PIVOT operator, but the title of the video says it does!

471FC61809 on 5/15/2009
The video doesn't show any things about using PIVOT operator.

Steve Harris on 5/15/2009
expand video contrasting CASE method with PIVOT method

Plamen Ratchev on 5/15/2009
This video demonstrates pivoting using the CASE expression. The same example using the PIVOT operator is available here: http://www.jumpstarttv.com/pivot-using-the-pivot-statement_525.aspx

Raghu ram on 5/15/2009
Useful video FOR DBA

Kenneth Wymore on 5/15/2009
Very nice example of using CASE statements to pivot data. Before SQL 2005 came out, I had to use this sort of logic all the time for reporting. I still use CASE statements alot because the syntax is easier to understand and it is more flexible than using a PIVOT command. I also like how you can have multiple aggregates for each grouped column using CASE statements whereas when you use the PIVOT command you can only have one aggregate column. Nice Work!

Leigh Anne on 5/15/2009
Very helpful! Thank you!

34058CB695 on 5/15/2009
hard to understand the speaker at first - garbled

Tiffany Franks on 5/15/2009
a little hard to understand with the speakers accent

Angier Crawford on 5/15/2009
That is exactly what I needed to see.

Christopher Buller on 5/15/2009
I believe I can also use this in SSIS, using case and pivot to create outputs with simple SQL query

Leonard Peoples on 5/15/2009
Excellent video. Keep 'em coming.

Plamen Ratchev on 5/15/2009
Ian, the scripts for the video are attached under References now, you can download and print.

Plamen Ratchev on 5/15/2009
Lou, you probably mean unpivoting. See the following videos: http://www.jumpstarttv.com/unpivot-data-using-the-unpivot-function-in-sql-server_542.aspx and http://www.jumpstarttv.com/unpivot-data-with-a-cross-join-in-sql-server_541.aspx.

Jose Horton on 5/15/2009
Excelent explanation

datamama on 5/15/2009
would be great to be able to print the code

Ümit Güler on 5/16/2009
thank u for sharing ur knowledge

Ahmad Elayyan on 5/18/2009
good

Carla Wilson on 5/18/2009
datamama, check the references box to the right of the video for the sql script (this is where they post code examples, etc. to go with the videos).

ML Stephens on 5/18/2009
I can put this use today

Ruthanne Vice on 5/22/2009
I found the video to be very clear and easy to understand - thanks I iwll use it today

Ted Quan on 7/28/2009
good

race on 10/22/2009
Good video. Wish we could get a follow up with more details on why the presenter decided to do it this particular way and not other ways.

Tonci on 10/23/2009
Great tsql souce code explanation! Thank you.

Plamen Ratchev on 10/24/2009
Race, using CASE is just one way to do pivoting. This was the point of the video, to demonstrate this technique. Here are a few reasons to use CASE expressions for pivoting: 1). It works with versions prior to SQL Server 2005 (since the PIVOT operator was introduced in SQL Server 2005); 2). It uses standard SQL and can be portable to other databases; 3). Using CASE expressions you can pivot on multiple columns, while the PIVOT operator allows pivoting only on a single column.

James Lawrence on 2/24/2010
Very concise and straight to the point...good!

John Torrey on 4/3/2010
I would love to have an explanation of the syntax so that I understand it, rather than just copying what I see here and using it as is. Where can I get an explanation? Also, is it possible to combine the pivot data with other data from other normalized tables via foreign keys so that in this case you get, let's say, five columns of relevant data in addition to the 3 month columns?

Plamen Ratchev on 4/4/2010
John, yes - you can reference other columns in the pivoting statement. They have to be included in the GROUP BY clause. Here is my blog post with more details on pivoting and examples: http://pratchev.blogspot.com/2007/04/pivoting-data-in-sql-server.html. In essence the CASE expression is used to filter only the relevant data for the particular column. All other values are ignored. This "spreads" the row data into columns.

Scott on 8/12/2010
Being a beginning DBA and reporting services analyst for our company, manipulating the data for end user needs is a must. Videos that provide examples of data manipulation is a real good way to learn new techniques and get different view points on this. Keep up the good work!

Tim on 8/12/2010
Good topic but the accent was a little strong. It was a little hard to understand some words he was saying.

Jonathan Skinner on 8/13/2010
I am already using this technique extensively and thought the video explained it very succinctly

John Barone on 8/13/2010
Thanks. I learned something new today.

Steve Culshaw on 8/25/2010
Nice run through

gopi krishna on 9/13/2010
NICE

lucas on 9/21/2010
I often used this kind of technique with CASE-statements (I admit I don't even know how to use the new PIVOT-statement!), this gives a nice summarize of it. thx

manjeet on 10/6/2010
Good for starters

Terrance on 10/24/2010
Very good video.

Rajeev on 10/28/2010
GOOD INTERACT

Sabarinathan on 11/11/2010
Good for all stagers

swetha on 12/17/2010
Good Video to share as PIVOT is not supported in 2000 or lowerversions.



Must Be Logged In
 

How Do I Become a Video Author? |  Newsletter History

Copyright © Fourdeuce, Inc., 2005-2009. All Rights Reserved | Privacy Policy | Terms & Conditions