I want to update the value of a column that is grouped by using as criteria two other columns. assume table A (a Char, b integer, c interger,d) and the a column can include same object name, so i want to update the table with the condition: if column’s a value and b value are found in two rows row1:(A1,2,numbery) and row2:(A1,2,numberx) then if: numberx,numbery>=0->AVG(third column), elsif numberx,numbery<0 THEN MIN else MAX (i.e -3,10->10).
--a--|--b--|--c--|--d-- desired TABLE update (A1,A2) --a--|--b--|--c--|--d--
A1 2 10 b A1 2 "15" b
A2 7 -9 bc A2 7 "4" bc
A3 7 12 fg A3 7 12 fg
A1 2 20 sa A1 2 "15" sa
A2 7 4 sa A2 7 "4" sa
Create tables and sample data:
To help figure out update, create a query that gives the desired results, as I understand them: (Not part of the solution, just part of solving)
Use
mergeto make available the agregate values during an update:Rollback to initial test data:
This query will not update rows where a change is not actually being made, reducing locking, undo and redo: