SQL同一个表的某字段值相加赋值给另一个字段

2024年11月23日 02:43
有5个网友回答
网友(1):

方法1:
我的是insert into 但不是别的表!
Insert Into A2
Select t.TABLRELA,t.D019,Sum(t.D019)Over(Partition By t.TABLRELA) From A2 t ;
Commit;
Delete A2 t Where Rowid Not In (Select Max(Rowid) From A2 y Where t.TABLRELA=y.TABLRELA And t.D019=y.D019);
Commit;
方法2:
update A2 s set s.D011 =(Select u.D011 From (Select t.TABLRELA,t.D019,Sum(t.D019)Over(Partition By t.TABLRELA) D011 From A2 t) u Where s.TABLRELA=u.TABLRELA And s.D019=u.D019);
Commit;

网友(2):

UPDATE A2 SET D019=B.D019
FROM A2 A,(SELECT SUM(D019),tablrela from A2 GROUP BY D019,tablrela) B
WHERE A.tablrela=B.tablrela

网友(3):

update a2 set d011=c.a
from (select sum(d019) as a,tablrela from a2 group by tablrela) c on c.tablrela=a2.tablrela

网友(4):

update as set d011=c.a
from (select sum(d019) as a from a2 a, a2 b where a.tablrela=b.tablrela)

网友(5):

试试这个
update a2 A set A.d011=(select sum(d019) from a2 B group by tablrela) where A.tablrela=B.tablrela