I'm trying to update a column in a table with the max value of that column grouped by another column.
So for example, say we have a table named transactions with two columns: quantity
and item_name
. And for whatever reason we want to set quantity
equal to the maximum quantity
found for each item_name
.
I'm pretty stumped and bad at doing things like this in SQL, but here's what I have so far:
UPDATE transactions
SET
quantity = subquery.quantity
FROM (select max(quantity), item_name
from transaction group by item_name) AS subquery
WHERE and item_name = subquery.item_name;