当前位置: 代码迷 >> Sql Server >> 将一张表中的记录合并,该怎么解决
  详细解决方案

将一张表中的记录合并,该怎么解决

热度:40   发布时间:2016-04-27 12:59:04.0
将一张表中的记录合并
请问将图1
转变为图2
怎么做?

------解决方案--------------------
给你一种实现方法:
1,创建表2,
2, 写一个存储过程或者程序,将表1的内容写入表遥2
具体方法:A, 找出所有的id: select examineeId from table1 group by examineeId
b,开始一个循环
c, 以id为条件,找出这个id相关的信息(名字,各科成绩)
d, 生成sql语句,写入表二中。
e, 循环结束
------解决方案--------------------
SQL code
declare @sql nvarchar(1000)set @sql='select examineeId,examineeName,'select @[email protected]+'sum(case appraisalName when '''+appraisalName+''' then result else 0 end) as '+appraisalName+',' from (select distinct appraisalName from finalresult) tset @sql=LEFT(@sql,len(@sql)-1)+' from finalresult group by examineeId,examineeName'exec(@sql)
  相关解决方案