34,590
社区成员
发帖
与我相关
我的任务
分享
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
create table [tb]([entityname] varchar(8),[fzr] varchar(6))
insert [tb]
select '线路电气','李瓜' union all
select '线路结构','王威' union all
select '电气一次','肖飞' union all
select '技经','尹豫落' union all
select '电气二次','李选正' union all
select '通信','许杰'
select identity(int,1,1) as id,* into #temp from [tb]
declare @sql varchar(8000)
select @sql = isnull(@sql,'')+',max(case id when '+ltrim(id)+' then fzr else '''' end) as [n'+ltrim(id)+']
,max(case id when '+ltrim(id)+' then entityname else '''' end) as [e'+ltrim(id)+']'
from #temp
select @sql = 'select '+stuff(@sql,1,1,'')+' from #temp'
exec(@sql)
drop table #temp
-----------------------
李瓜 线路电气 王威 线路结构 肖飞 电气一次 尹豫落 技经 李选正 电气二次 许杰 通信
最好给出完整的表结构,测试数据,计算方法和正确结果.