码迷,mamicode.com
首页 > 数据库 > 详细

sql server 行转列

时间:2014-11-13 12:17:50      阅读:267      评论:0      收藏:0      [点我收藏+]

标签:ar   sp   on   bs   ef   as   sql   res   nbsp   

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)

 

sql server 行转列

标签:ar   sp   on   bs   ef   as   sql   res   nbsp   

原文地址:http://www.cnblogs.com/BeyondWang/p/4094474.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!