首页 > 代码库 > 行转列-高级
行转列-高级
Test 数据库
declare @sql varchar(8000)
declare @sql2 varchar(8000)
set @sql=‘‘
set @sql2=‘‘
select @sql=@sql+‘,‘+code from ExtentData group by code
select @sql2=@sql2+‘ ,(select max(‘+a.Code+‘) from data where data.RequestID=Data2.RequestID) as ‘+b.Name from ExtentData as a
left join ExtentProperty as b on a.Code=b.Code
group by a.code,b.Name
set @sql=stuff(@sql,1,1,‘‘)--去掉首个‘,‘
set @sql=‘ with data as ( select * from [ExtentData] pivot (max(Value) for code in (‘+@sql+‘) ) a) ,data2 as (
select RequestID ‘+@sql2+‘
from data as Data2 group by RequestID) select * from PatientSurgeryRequest as x left join data2 on x.ID=data2.RequestID ‘
print @sql
exec(@sql)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。