Search code examples
sqloracle-databasegroup-bysql-updatehaving

SQL UPDATE with having count


I had this table

A B
1 1H
2 1H
1 1G
1 1E

I need update on this table column A, that take max(a) group by B having count(*) = 2.

Result will be like this:

A B
2 1H
2 1H
1 1G
1 1E

Solution

  • Perhaps?

    update tablename t1
    set A = (select max(A) from tablename t2 where t2.B = t1.B)
    where B in (select B from tablename group by B having count(*) >= 2)