日期:2014-05-17 浏览次数:20595 次
select a.name,a.shj,a.other,
case b.wnum when 1 then b.memo else null end as wnum1,
case b.wnum when 2 then b.memo else null end as wnum2,
case b.wnum when 3 then b.memo else null end as wnum3,
case b.wnum when 4 then b.memo else null end as wnum4
from 表A a,表B b
where a.id=b.Aid
declare @sql nvarchar(max)
select @sql=isnull(@sql,'')+',max(case when wnum='+rtrim(wnum)+' then memo else null end)wnum'+rtrim(wnum) from(select distinct wnum from b)t
print @sql
exec('select name,shj,other'+@sql+' from a join b on a.id=b.aid group by name,shj,other')