UPDATE W SET W.A = ( SELECT W.A - S.A from S where W.G = S.G and
S.N in(SELECT max(N) FROM S group by G ) )
where exists (select 1 from S where W.G = S.G and
S.N in(SELECT max(N) FROM S group by G ) )
exists 语句很重要,否则如果出现么有匹配上的时候会把其他数据更新掉。
标准sql,sqlserver oracle 都可以
sqlserver:UPDATE W SET W.A = W.A - S.A FROM S WHERE W.G = S.G and S.N in
(SELECT max(N) FROM S group by G)
select column_a,count(*) from table_b
group by column_a
having count(*)>1