ssshehu
ssshehu

Reputation: 9

Sum and update group row using mysql

I am having a difficulty in summing and updating those row amount to one row of the same receiving column.

please can someone help me. here is the image.

enter image description here.

Upvotes: 0

Views: 37

Answers (2)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520968

If I understand your requirement, you want to delete all records from the original table, leaving only one per receiver. And you want to update the amount for that remaining record to the sum of amounts for all records, for that receiver. Rather than trying to deal with a complex update and delete operation, I might recommend that you just create a new table and insert the data you want into that:

CREATE TABLE new_table (receiver varchar(11), amount int);
INSERT INTO new_table (receiver, amount)
SELECT receiver, SUM(amount)
FROM original_table
GROUP BY receiver

Then, you can drop the original table since you don't need/want it anymore.

Upvotes: 1

Carl Binalla
Carl Binalla

Reputation: 5401

Try this

SELECT receiver, SUM(amount) as total
FROM table
GROUP BY receiver

This will add all amount with the same receiver

Upvotes: 1

Related Questions