日期:2014-05-18 浏览次数:20464 次
declare @sql varchar(8000) select @sql = isnull(@sql + ',' , '') + Class from Elearning_AnswerSheet group by Class exec ('select m.* , n.平均分 from (select * from (select Store,Name,Code,Mark from Elearning_AnswerSheet) a pivot (AVG(Mark) for Class in (' + @sql + ')) b) m , (select Store,Name,Code,avg(Mark) as 平均分 from Elearning_AnswerSheet group by Store,Name,Code) n where m.Store = n.Store and m.Name=n.Name and m.Code=n.Code) tb ') --try
------解决方案--------------------
declare @sql varchar(8000) select @sql = isnull(@sql + '],[' , '') + Class from Elearning_AnswerSheet group by Class set @sql = '[' + @sql + ']' exec ('select m.* , n.平均分 from (select * from (select Store,Name,Code,Mark from Elearning_AnswerSheet) a pivot (AVG(Mark) for Class in (' + @sql + ')) b) m , (select Store,Name,Code,avg(Mark) as 平均分 from Elearning_AnswerSheet group by Store,Name,Code) n where m.Store = n.Store and m.Name=n.Name and m.Code=n.Code')