August 14, 2009 at 7:19 am
I have a table of class meeting times and I'd like to have the meeting times be column headers. Does anyone know how I can do that?
ex:
I have
Class Name Meeting Time
Art 1/1/09
Art 1/3/09
Art 1/7/09
.
.
.
What I'd like is
Class Name 1/1/09 1/3/09 1/7/09
Art blank blank blank
Mike Scalise, PMP
https://www.michaelscalise.com
August 14, 2009 at 7:33 am
Here are links to 2 articles that explain how to do what you want:
http://www.sqlservercentral.com/articles/T-SQL/63681/
http://www.sqlservercentral.com/articles/Crosstab/65048/
The second one builds on the first so make sure you read them in order.
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
August 14, 2009 at 7:34 am
Quite a common question within this forum:
http://www.sqlservercentral.com/articles/T-SQL/63681/
BrainDonor
August 14, 2009 at 8:05 am
It seems when using PIVOT, you have to specify the column names, but given that I will have many different dates in my table, how can I specify those column names? I won't know how many columns there will be. It depends on how many dates that are returned.
Mike Scalise, PMP
https://www.michaelscalise.com
August 14, 2009 at 10:34 am
Since you don't know how many column names there will be in advance, you must do it all with dynamic SQL. Essentially, you pull a list of all the values that will be transformed into columns, then write either the PIVOT statement or crosstab statements and execute them on the fly.
__________________________________________________
Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply