SQL同一个表的某字段值相加赋值给另一个字段
有一个表,A2,例如,字段有D011,D019,tablrela101121122333我想用个命令可以根据相同的tablrela的值将sum(D019)赋值到D011上...
有一个表,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 去新建个副表,我这里有权限控制,只能在同一个表里完成 展开
字段有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 去新建个副表,我这里有权限控制,只能在同一个表里完成 展开
5个回答
展开全部
方法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;
我的是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;
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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
FROM A2 A,(SELECT SUM(D019),tablrela from A2 GROUP BY D019,tablrela) B
WHERE A.tablrela=B.tablrela
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
update a2 set d011=c.a
from (select sum(d019) as a,tablrela from a2 group by tablrela) c on c.tablrela=a2.tablrela
from (select sum(d019) as a,tablrela from a2 group by tablrela) c on c.tablrela=a2.tablrela
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
update as set d011=c.a
from (select sum(d019) as a from a2 a, a2 b where a.tablrela=b.tablrela)
from (select sum(d019) as a from a2 a, a2 b where a.tablrela=b.tablrela)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
试试这个
update a2 A set A.d011=(select sum(d019) from a2 B group by tablrela) where A.tablrela=B.tablrela
update a2 A set A.d011=(select sum(d019) from a2 B group by tablrela) where A.tablrela=B.tablrela
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询