求助,在SQL数据库中如何将一个表中某几列单元格求和后的数据,更新到另一张新表中
请问,如何将dbo.Clicks中的同一CodeID的Clicks的值求和,并插入到dbo.BusCode表中对应CodeID的Views中,如果Views中已经有值,把那个和跟这个旧值相加,插入到Views中
最好能用update命令一句话执行
[解决办法]
--更新
;with cte as(
select codeid as cid,sum(clicks) as csum from dbo.clicks
group by codeid
)
update dbo.BusCode set views=views+csum from cte where codeid=cid
update dbo.BusCode
set views=views+csum
from
(
select codeid,sum(clicks) as csum
from dbo.clicks
group by codeid
)t
where t.codeid= BusCode.codeid
UPDATE dbo.BusCode
SET dbo.BusCode.VIEWS=dbo.BusCode.VIEWS+b.clicks
FROM (
SELECT codeid,SUM(clicks)clicks
FROM dbo.Clicks
GROUP BY codeid)b
WHERE dbo.BusCode.codeid=b.codeid