sql中的group by和having如何使用

2024-12-27 18:30:39
推荐回答(3个)
回答1:

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 都可以

回答2:

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)

回答3:

select column_a,count(*) from table_b
group by column_a
having count(*)>1