DECLARE @cols As nvarchar(max), @query As nvarchar(MAX);
select @cols = STUFF((SELECT distinct ',' + QUOTENAME(agent_first_name) FROM #Temp2
FOR XML PATH(''), TYPE
).value('.', 'nvarchar(max)')
,1,1,'');
Set @query ='Select question_text, ' + @cols +'
From
(
Select question_text, agent_first_name,avgcount
From #Temp2
) As SourceTable Pivot
(
avg(avgcount) For agent_first_name In (' + @cols + ')
) As PivotTable'
print @query
EXECUTE sp_executesql @query
In above query generate below results
question_text Arpita Kanan TestContact1
We are thorough in our approach to our work. 1.000000 1.0 ...
Go to the complete details ...