declare @s varchar(8000)
set @s=''
select @s=@s+',['+status+']=sum(case status when '''+status+''' then [sum] end)'
from 第二个表
exec('select style'+@s+' from 第一个表 group by style')
set @s=''
select @s=@s+',['+status+']=sum(case status when '''+status+''' then [sum] end)'
from 第二个表
exec('select style'+@s+' from 第一个表 group by style')
select
style,
sum(case status when '未处理' then [sum] else 0 end) as 未处理,
sum(case status when '已经支付未收到' then [sum] else 0 end) as 已经支付未收到,
sum(case status when '已经支付且收到' then [sum] else 0 end) as 已经支付且收到,
from 表1 group by style