SQL Update to the SUM of its joined values

后端 未结 7 1457
北恋
北恋 2020-12-04 17:42

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         


        
7条回答
  •  情书的邮戳
    2020-12-04 18:14

    This is a valid error. See this. Following (and others suggested below) are the ways to achieve this:-

    UPDATE P 
    SET extrasPrice = t.TotalPrice
    FROM BookingPitches AS P INNER JOIN
     (
      SELECT
        PitchID,
        SUM(Price) TotalPrice
      FROM
         BookingPitchExtras
      GROUP BY PitchID
      ) t
    ON t.PitchID = p.ID
    

提交回复
热议问题