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

有一个表,A2,例如,
字段有D011,D019,tablrela
10 1
12 1
1 2
2 3
3 3
我想用个命令可以根据相同的tablrela的值将sum(D019)赋值到D011上
结果是D011,D019,tablrela
22 10 1
22 12 1
1 1 2
5 2 3
5 3 3
我要的语句是在同一个表里,这个我自己写的update a2 set d011=(select sum(d019) from a2 where tablrela=tablrela)
不知道哪里错了,请别写成先用SELECT * into 去新建个副表,我这里有权限控制,只能在同一个表里完成

第1个回答  2011-08-19
方法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个回答  2011-08-19
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个回答  2011-08-19
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个回答  2011-08-19
update as set d011=c.a
from (select sum(d019) as a from a2 a, a2 b where a.tablrela=b.tablrela)
第5个回答  2011-08-19
试试这个
update a2 A set A.d011=(select sum(d019) from a2 B group by tablrela) where A.tablrela=B.tablrela
相似回答