22,209
社区成员
发帖
与我相关
我的任务
分享
UPDATE aa SET aa.je1=cc.type1,aa.je2=cc.type2,aa.je3=cc.type3,aa.je4=cc.type4,aa.je5=cc.type5,aa.je6=cc.type6,aa.je7=cc.type7
FROM a AS aa
INNER JOIN (
select * from
(SELECT aid,iType,je FROM b) bb
PIVOT(SUM(je) FOR iType IN (type1,type2,type3,type4,type5,type6,type7)) u
) cc ON aa.id=cc.aid
-- 写成这们的,只要扫描一次表 b 就可以了
with m as (
select
aid,
sum(case when iType='type1' then je else 0 end) as je1,
sum(case when iType='type2' then je else 0 end) as je2,
sum(case when iType='type3' then je else 0 end) as je3,
sum(case when iType='type4' then je else 0 end) as je4,
sum(case when iType='type5' then je else 0 end) as je5,
sum(case when iType='type6' then je else 0 end) as je6,
sum(case when iType='type7' then je else 0 end) as je7
from b
group by aid
)
update a
set je1 = m.je1, je2 = m.je2, je3 = m.je3, je4 = m.je4, je5 = m.je5, je6 = m.je6, je7 = m.je7
from m
where a.id = m.aid
go