I tried implementing the following SQL code (SQL Server 2008) but keep getting the syntax issue while trying to PIVOT the tables using the similar code in my earlier post and I am stumped.
http://sqlfiddle.com/#!3/100bd/4
Error:
Incorrect syntax near ','.: DECLARE @query AS NVARCHAR(MAX), @colsPivot as NVARCHAR(MAX) select @colsPivot = STUFF((SELECT ',' + quotename('MEM'+ cast(MEMPER_Instance as varchar(10))+''+REPLACE(c.name, 'MEMPER', '')) from MEM_TEMP t cross apply sys.columns as C where C.object_id = object_id('MEM_TEMP') and C.name not in ('MEMPER_Instance','AHS_ID') group by t.MEMPER_Instance, c.name order by t.MEMPER_Instance FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)') ,1,1,'') set @query = ' SELECT * into #temp from ( select ahs_id, ''MEM''+cast(MEMPER_instance as varchar(10))+''_''+col col, value from ( select ahs_id, MEMPER_Instance, ,cast (MEMPER_MinValue as float) MinValue ,cast (MEMPER_MaxValue as as float)) MaxValue ,cast (MEMPER_AvgValue as float) AvgValue ,cast (MEMPER_MedianValue as float) MedianValue ,cast (MEMPER_Count as as float) Count from MEM_TEMP ) x unpivot ( value for col in ( MinValue, MaxValue, AvgValue, MedianValue, Count ) ) u ) x1 pivot ( max(value) for col in ('+ @colspivot +') ) p ; select * from #temp' exec(@query)
How can I identify the problem?