SQL Update to the SUM of its joined values

后端 未结 7 1462
北恋
北恋 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:15

    Use a sub query similar to the below.

    UPDATE P
    SET extrasPrice = sub.TotalPrice from
    BookingPitches p
    inner join 
    (Select PitchID, Sum(Price) TotalPrice
        from  dbo.BookingPitchExtras
        Where [Required] = 1
        Group by Pitchid
    ) as Sub
    on p.Id = e.PitchId 
    where p.BookingId = 1
    

提交回复
热议问题