含有多重含义字段的纵横表切换
TA表如下:商品销售和退货在一起,用vtype区别:30表示退货,31表示销售
- HTML code
proname qty vtype AA -10 30AA 21 31AA 12 31AA -2 30BB -1 30BB 90 31 ...
用Sql怎么切换成
- HTML code
proname xssl xtsl AA 33 -12 BB 90 -1...
期望高手帮忙
[解决办法]
- SQL code
godeclare @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 @strexec('select [proname]'+@str+' from tbl group by [proname]')/*proname 30 31AA -12 33BB -1 90*/