Link to home
Start Free TrialLog in
Avatar of Scamquist
ScamquistFlag for United States of America

asked on

Create Pivot Table (crosstab) in SQL 2008

I have a table    WeekTaskStatus  with Task WeekEnd and TaskStatus
Task      WeekEnd      TaskStatus
Budget      4/24/2009      Green
Resources      4/24/2009      Yellow
Scope      4/24/2009      Red
Timeline      4/24/2009      Yellow
Budget      5/1/2009      Yellow
Resources      5/1/2009      Green
Scope      5/1/2009      Red
Timeline      5/1/2009      Yellow
Budget      5/8/2009      Green
Resources      5/8/2009      Red
Scope      5/8/2009      Yellow
Timeline      5/8/2009      Green
Budget      5/15/2009      Green
Resources      5/15/2009      Yellow
Scope      5/15/2009      Red
Timeline      5/15/2009      Yellow
Budget      5/22/2009      Yellow
Resources      5/22/2009      Red
Scope      5/22/2009      Red
Timeline      5/22/2009      Yellow

I am trying to create an SQL pivot to display the data as shown below
      Budget      Resources      Scope      Timeline
4/24/2009      Green      Yellow      Red      Yellow
5/1/2009      Yellow      Green      Red      Yellow
5/8/2009      Green      Red      Yellow      Green
5/15/2009      Green      Yellow      Red      Yellow
5/22/2009      Yellow      Red      Red      Yellow

The number of Tasks will increase, so the pivot needs to account for the future additional columns.
There will only be one TaskStatus per WeekEnding per Task.
Is this possible?
Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

You can look at the PIVOT keyword for SQL keeping in mind that it  requires hard coding of column names (values) to be pivoted.

Here is a nice article by Tim Chapman on the subject:
http://articles.techrepublic.com.com/5100-10878_11-6143761.html

Here is a solution from Mark Wills here on EE:
https://www.experts-exchange.com/articles/Microsoft/Development/MS-SQL-Server/SQL-Server-2005/Dynamic-Pivot-Procedure-for-SQL-Server.html
Avatar of Scamquist

ASKER

Once the solution is in place, we may add additional tasks.  I would be too cumbersome ot go back and hard code new column names.

I have been looking for awhile and have seen references to having the column created dynamically.  
Yes, using dynamic SQL.  Take a look at the second link I posted from Mark talking about that.  His article is providing a nice dynamic SQL procedure which will do this for you OR you can at least learn to build a similar implementation.
ASKER CERTIFIED SOLUTION
Avatar of ralmada
ralmada
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think we are almost there,

The code you provided returned the column header in the body.  
I changed line 15 to
pivot (Max(TaskStatus) for Task in (' + @cols + ')) p'

which inserted the Red, Yellow or Green in the body :)  

However, I get and Error Box:
Error - SQL Syntax Errors Encountered
The following errors were encountered while parsing the contenst of the SQL pane.
The Declare SQL construct or statement is not supported.

If I ignore, the query runs, but I can't save it.

Ideas?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you both.  I have been burning up the internet trying to find the answer.  The only answers I found used many dozens of lines of code.  Very complicated.  Thank you so much