update a set a.cc = b.bb
from prdt a
join (
Select aa,sum(bb) as bb from prdt group by aa
) b on a.aa = b.aa
where IsNull(a.cc,0) = 0
from prdt a
join (
Select aa,sum(bb) as bb from prdt group by aa
) b on a.aa = b.aa
where IsNull(a.cc,0) = 0
调试欢乐多
update a set a.cc = b.bb
from prdt a
join (
Select aa,sum(bb) as bb from prdt group by aa
) b on a.aa = b.aa
where a.cc is null
from (select sum(bb) bb,aa from tablename where aa=b.aa group by aa) a
where b.cc is null and tablename .aa=a.aa
String aa;
int bb;
int cc;
现在要将aa相同的所以记录的bb的值相加,把总数放在相同aa且cc为空的记录中。请问如何实现?
数据例如:
aa bb cc
ss 1
ss 3 4
vv 55
vv 2 6
vv 6 8
结果为:
aa bb cc
ss 4
ss 3 4
vv 63
vv 2 6
vv 6 8 create table prdt(aa varchar(100),bb int,cc int)
insert into prdt select 'ss' , 1 ,null
insert into prdt select 'ss', 3, 4
insert into prdt select 'vv', 55,null
insert into prdt select 'vv', 2, 6
insert into prdt select 'vv', 6, 8
goupdate a
set a.bb=a.bb+b.sums
from prdt as a join (select aa,sum(bb) as sums
from prdt
where cc is not null
group by aa) as b
on a.aa=b.aa
where a.cc is null
insert into test_table select 'ss' , 1 ,null
insert into test_table select 'ss', 3, 4
insert into test_table select 'vv', 55, null
insert into test_table select 'vv', 2, 6
insert into test_table select 'vv', 6, 8update test_table
set bb=b.bb
from (select aa as df,sum(bb) bb from test_table group by aa) b
where aa=b.df and cc is null
update a set a.bb = b.bb
from test_table a
join (Select aa,sum(bb) as bb from test_table group by aa) b
on a.aa = b.aa
where a.cc is null