2

I have the following query using PostgreSql :

SELECT SUM(table.a) 
FROM table 
GROUP BY table.b
HAVING SUM(table.a) > x;

And now I need to update a column in all rows affected by the precedent query.

I tried the following solution :

UPDATE table 
SET c = 'value' 
WHERE (SELECT SUM(table.a) 
       FROM table 
       GROUP BY table.b) > x;

but I get the following error

More than a line returned by a sub-request used as an expression

I cannot find a solution to update a column in all rows affected by a group by. If anyone can show me the way, it would be greatly appreciated.

Vetouz
  • 159
  • 3
  • 19

1 Answers1

2

You want to update table rows only for those rows that have table.b in the list of values of those whose sums of table.a column values exceeded your defined x

So, I believe you want to use this:

UPDATE TABLE
   SET c = 'value'
 WHERE b IN (SELECT b
               FROM TABLE
              GROUP BY table.b
             HAVING SUM (table.a) > x));
Goran Kutlaca
  • 2,014
  • 1
  • 12
  • 18