日期:2014-05-17 浏览次数:20476 次
DECLARE @sql VARCHAR(max),@col_name VARCHAR(max) SELECT @sql=ISNULL(@sql+',','')+QUOTENAME([topics]) FROM tb GROUP BY [topics] SELECT @col_name=ISNULL(@col_name+',','')+'CAST('+QUOTENAME([topics])+' AS DECIMAL(9,2)) AS '+QUOTENAME([topics]) FROM tb GROUP BY [topics] EXEC(' SELECT tid,'+@col_name+' FROM ( SELECT topics,topics_scores=topics_scores*1.0,tid FROM tb )a PIVOT (AVG([topics_scores]) FOR [topics] IN('+@sql+')) b ')