Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
scamquist
Starting Member
2 Posts |
Posted - 2009-07-08 : 11:20:04
|
| I have a table (WeekTaskStatus) with three column: Task, WeekEnd and TaskStatus.There are currently three tasks (Scope, Budget and Resources) that are updated weekly with the values Green, Yellow and Red.Task WeekEnd TaskStatus--------------------------------------------Scope 1/1/2009 Red Scope 1/8/2009 GreenScope 1/15/2009 GreenBudget 1/1/2009 YellowBudget 1/8/2009 RedBudget 1/15/2009 GreenResources 1/1/2009 GreenResources 1/8/2009 RedResources 1/15/2009 YellowThe Stored Procedure uses dynamic SQL below creates a pivot table with the Task as the column headers, the WeekEnd as the rows and the TaskStatus as the data. Scope Budget Resources--------------------------------------------1/1/2009 Red Yellow Green1/8/2009 Green Red Red1/15/2009 Green Green Yellow...I need to use dynamic SQL because additional tasks may be created and would need to show the additional columns as needed. For example, ModificationReview may be added 1/22/2009.Is it possible to add a statement in the Stored Procedure to create a table (not temp table), and overwrite when the data changes?ALTER PROCEDURE dbo.TaskStatusPivotas Declare @strSQL varchar(max)DECLARE @cols varchar(2000)SELECT @cols = STUFF(( SELECT DISTINCT TOP 100 PERCENT '],[' + task FROM WeekTaskStatus ORDER BY '],[' + task FOR XML PATH('') ), 1, 2, '') + ']' set @strSQL = 'select WeekEnd, ' + @cols + ' from ( select WeekEnd, Task, TaskStatus from WeekTaskStatus) o pivot (Max(TaskStatus) for Task in (' + @cols + ')) p' exec(@strSQL)Any help will be appreciated.Steve |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-07-08 : 12:54:21
|
quote: Originally posted by scamquist I have a table (WeekTaskStatus) with three column: Task, WeekEnd and TaskStatus.There are currently three tasks (Scope, Budget and Resources) that are updated weekly with the values Green, Yellow and Red.Task WeekEnd TaskStatus--------------------------------------------Scope 1/1/2009 Red Scope 1/8/2009 GreenScope 1/15/2009 GreenBudget 1/1/2009 YellowBudget 1/8/2009 RedBudget 1/15/2009 GreenResources 1/1/2009 GreenResources 1/8/2009 RedResources 1/15/2009 YellowThe Stored Procedure uses dynamic SQL below creates a pivot table with the Task as the column headers, the WeekEnd as the rows and the TaskStatus as the data. Scope Budget Resources--------------------------------------------1/1/2009 Red Yellow Green1/8/2009 Green Red Red1/15/2009 Green Green Yellow...I need to use dynamic SQL because additional tasks may be created and would need to show the additional columns as needed. For example, ModificationReview may be added 1/22/2009.Is it possible to add a statement in the Stored Procedure to create a table (not temp table), and overwrite when the data changes?ALTER PROCEDURE dbo.TaskStatusPivotas drop table yourdestinationtableDeclare @strSQL varchar(max)DECLARE @cols varchar(2000)SELECT @cols = STUFF(( SELECT DISTINCT TOP 100 PERCENT '],[' + task FROM WeekTaskStatus ORDER BY '],[' + task FOR XML PATH('') ), 1, 2, '') + ']' set @strSQL = 'select WeekEnd, ' + @cols + ' into yourdestinationtable from ( select WeekEnd, Task, TaskStatus from WeekTaskStatus) o pivot (Max(TaskStatus) for Task in (' + @cols + ')) p' exec(@strSQL)Any help will be appreciated.Steve
yup you can just add a into table name on dynamic sql string |
 |
|
|
scamquist
Starting Member
2 Posts |
Posted - 2009-07-08 : 16:23:14
|
| It worked. Thank you for the assistSteve |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-07-09 : 10:50:44
|
| welcome |
 |
|
|
|
|
|
|
|