别人叫我阿汤哥_汤姆克鲁斯请进,刚刚那个求平均分求教
有点小问题,因为题目是随机从题库里面抽出来的,就是投票的时候,投了别的题目,而该Tid没有分数的话
平均分就变成NULL了,但是题目却还是显示了出来。
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 )aPIVOT (AVG([topics_scores]) FOR [topics] IN('+@sql+')) b')