码迷,mamicode.com
首页 > 其他好文 > 详细

行转列-高级

时间:2014-08-05 18:56:09      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:数据   for   ar   数据库   sql   har   ef   rom   

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)

 

行转列-高级,布布扣,bubuko.com

行转列-高级

标签:数据   for   ar   数据库   sql   har   ef   rom   

原文地址:http://www.cnblogs.com/ifreesoft/p/3892747.html

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