Technomancy

SQL - Updating each row with a grouped by total

written by rory, on Mar 23, 2009 10:45:00 PM.

Let's pretend you have an SQL table like this:
NameDonationTotal Amount
Alice10
Alice12
Alice8
Alice9
Bob2
Bob3
and you want to fill in the column with the total for each person based on the Donation column You can use the following SQL command:
UPDATE table SET "Total Amount" = x.total from ( SELECT "Name", SUM("Amount") AS total FROM table GROUP BY Name ) AS x WHERE x.Name = Name;

Comments

  • You know so many interesting infomation. You might be very wise. I like such people. Don't top writing.

    Comment by KonstantinMiller — Jul 7, 2009 1:51:55 AM | # - re

Leave a Reply