第2个回答 2010-08-09
update table2 set v1=(select v1 from (select name,count(v1) v1,count(v2) v2 from table1 group by name having count(v1)>=2 and count(v2)>=2) b where a.name=b.name);
update table2 set v2=(select v2 from (select name,count(v1) v1,count(v2) v2 from table1 group by name having count(v1)>=2 and count(v2)>=2) b where a.name=b.name);
分两步写
第3个回答 2010-08-09
declare @tb1 table ([name] varchar(10),v1 int,v2 int)
insert into @tb1 select 'qb',1,1
insert into @tb1 select 'qb',2,2
insert into @tb1 select 'qb',3,3
insert into @tb1 select 'zw',1,1
insert into @tb1 select 'zw',2,1
insert into @tb1 select 'zw',3,3
insert into @tb1 select 'zw',4,4
declare @tb2 table([name] varchar(10),v1 int,v2 int)
insert into @tb2 select 'qb',0,0
insert into @tb2 select 'zw',0,0
update cc
set cc.v1=c1,cc.v2=c2
from @tb2 cc join (
select [name],count(v1) as c1 from @tb1 a where v1>=2 group by [name]) aa on cc.[name]=aa.[name]
left join (select [name],count(v2) as c2 from @tb1 where v2>=2 group by [name]) bb on aa.[name]=bb.[name]
select * from @tb2
第4个回答 2010-08-09
sql server :
update table2
set v1=b.v1,v2=b.v2
from table2 a left join
(select name,sum(v1) v1,sum(v2) v2 from table1 group by name having sum(v1)>=2 and sum(v2)>=2) b on a.name=b.name
where b.name is not null