I'm trying to update a field in the database to the sum of its joined values:
UPDATE P
SET extrasPrice = SUM(E.price)
FROM dbo.BookingPitchExtras AS E
INNER JOIN dbo.BookingPitches AS P ON E.pitchID = P.ID
AND P.bookingID = 1
WHERE E.[required] = 1
When I run this I get the following error:
"An aggregate may not appear in the set list of an UPDATE statement."
Any ideas?
You need something like this :
I ran into the same issue and found that I could solve it with a Common Table Expression (available in SQL 2005 or later):
Use a sub query similar to the below.
With postgres, I had to adjust the solution with this to work for me:
An alternate to the above solutions is using Aliases for Tables:
This is a valid error. See this. Following (and others suggested below) are the ways to achieve this:-