日期:2014-05-18 浏览次数:20511 次
proname qty vtype AA -10 30 AA 21 31 AA 12 31 AA -2 30 BB -1 30 BB 90 31 ...
proname xssl xtsl AA 33 -12 BB 90 -1 ...
go declare @str varchar(max) set @str='' select @str=@str+','+'['+LTRIM([vtype])+']'+'=sum(case when [vtype]='+ ltrim([vtype])+' then [qty] else 0 end)' from tbl group by [vtype] print @str exec('select [proname]'+@str+' from tbl group by [proname]') /* proname 30 31 AA -12 33 BB -1 90 */