DECLARE @sql varchar (8000), @groupby varchar (8000)
SET @sql = ' Select B.name,c.username,c.mobile,a.[no],convert (varchar (ten), a.createdate,121) as CreateDate '
Set @groupby = ' '
--Display the grouped columns
SELECT @sql = @sql + ', max (case A.eid "+convert (varchar), A.eid)
+ "then A.myanswer else" "End" [' + CONVERT (varchar), A.eid) + '] '
From (SELECT DISTINCT
EID
From Webcastexamresult
) as a
--column of the following group by
--select @groupby = @groupby + ', Case A.eid when ' +convert (varchar), A.eid)
--+ ' then a.myanswer Else ' ' End '
--from (SELECT DISTINCT
--EID
-From Webcastexamresult
--) as a
SET @sql = @sql + ', sum (case isright if 1 then 1 else 0 end) Isright from dbo. Webcastexamresult A
Left join Config B on A.cid=b.id
Left joins [User] C on A.createuserid=c.id GROUP by B.name,c.username,c.mobile,a.[no],convert (varchar), a.createdate, 121) '
Print @sql
EXEC (@sql)
SQL Server row to column