Okay I switched things up and can use a Pivot table but need to add a where clause to it. Does anyone know about Pivot tables?The error message I'm getting is from the last line in front of the Exec SQL stating...Incorrect syntax near ' (where weekdat=06/26/2009);'DECLARE @listCol VARCHAR(2000)SELECT @listCol = STUFF(( SELECT DISTINCT '],[' + convert(varchar(10),(Weekdat),1) --converting the weekdat FROM DiaryPct --where (dateadd(ww, -14, getdate()) < weekdat) --getting 13 weeks of data out ORDER BY '],[' + convert(varchar(10),(weekdat),1) FOR XML PATH('') ), 1, 2, '') + ']';DECLARE @sql NVARCHAR(2000); SET @sql = N' WITH Totals AS ( SELECT sort, doc, dist, area, reg, CASE WHEN SUM(totovr)= 0 THEN 0 WHEN SUM(totpnd) = 0 THEN 0 ELSE (convert(decimal(6,1),(((((sum(totovr))) * 1.00) / (sum(totpnd))) * 100))) END AS total, weekdat FROM DiaryPctGROUP BY sort,doc, dist, area, reg, weekdat) SELECT sort,reg, ' + @listCol + N' ,doc, dist,area ' +--N' Into DiariesPivot ' +N'FROM (SELECT sort,doc, dist, area, reg,total, weekdat FROM Totals) as O ' + N' PIVOT ' + N'(SUM(total) FOR weekdat IN (' + @listCol + N')) AS P'; N' (where weekdat=06/26/2009);'EXEC(@sql);