sql - Find highest in a group and update a column based n that -
simplified example: table has 4 columns: id, name, amount , parent. see image.
the parent column, in actual table, empty , needs populated entries column "name".
id non unique column multiple entries in column 'name' having same id.
the goal find maximum value entry in column "amount" id , populate column "parent" entry in column "name". post picture of example table in comments
the technique rank names within each id amount in subquery , join source table update:
update my_table set "parent"="name" ( select id,"name",row_number() on (partition id order "amount" desc) my_table ) t my_table.id=t.id , t.row_number=1
Comments
Post a Comment