sql server 行转列
DECLARE @sql VARCHAR(8000),@groupby VARCHAR(8000)
SET @sql = ‘select B.name,C.username,c.mobile,A.[no],convert(varchar(10),A.createDate,121) as createDate‘
set @groupby=‘‘
--显示分组的列
SELECT @sql = @sql + ‘ , max(case A.EID when ‘‘‘ +convert(varchar(10), A.EID)
+ ‘‘‘ then A.myAnswer else ‘‘‘‘ end )[‘ + convert(varchar(10), A.EID) + ‘]‘
FROM ( SELECT DISTINCT
EID
FROM WebCastExamResult
) AS a
--后面group by 的列
--SELECT @groupby = @groupby + ‘ , case A.EID when ‘‘‘ +convert(varchar(10), A.EID)
-- + ‘‘‘ then A.myAnswer else ‘‘‘‘ end ‘
--FROM ( SELECT DISTINCT
-- EID
-- FROM WebCastExamResult
-- ) AS a
SET @sql = @sql + ‘,sum(case isright when 1 then 1 else 0 end) isright from dbo.WebCastExamResult A
left join Config B on A.CID=B.ID
left join [User] C on A.createuserid=C.Id group by B.name,C.username,c.mobile,A.[no],convert(varchar(10),A.createDate,121) ‘
print @sql
EXEC(@sql)
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。