0

Getting an error when running this

UPDATE FlightBooking
INNER JOIN Passenger ON Passenger.FlightBookingId=FlightBooking.FlightBookingId 
INNER JOIN AirplaneSeat ON AirplaneSeat.AirplaneSeatId =  Passenger.SeatId
INNER JOIN Section ON AirplaneSeat.SectionId = Section.SectionId
INNER JOIN ExtraCost ON ExtraCost.FlightBookingId=FlightBooking.FlightBookingId
INNER JOIN Luggage ON Luggage.LuggageId = ExtraCost.LuggageId
INNER JOIN SportsEquipment ON ExtraCost.SportsEquipmentId=SportsEquipment.SportsEquipmentId
INNER JOIN Insurance ON ExtraCost.InsuranceId = Insurance.InsuranceId
INNER JOIN CarHirePrice ON CarHirePrice.CarHirePriceId= ExtraCost.CarHirePriceId
INNER JOIN Route ON FlightBooking.RouteId = Route.RouteId
SET FlightBooking.TotalCost = (SUM(Section.PriceInflux+Route.RoutePrice+Luggage.Price+SportsEquipment.SportsEquipmentPrice+Insurance.Price+CarHirePrice.TotalPrice)) 
WHERE FlightBooking.FlightBookingId=1;

When I have it formed as a Select query it returns the correct value so all the tables are fine. I'm assuming my syntax is wrong.

Any help would be appreciated.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

3 Answers3

0

You should use the following syntax when using an aggregate in an update statement.

UPDATE t1
 SET t1.field = t2.field2Sum
 FROM table1 t1
 INNER JOIN (select field3, sum(field2) as field2Sum
    from table2
       group by field3) as t2
 on t2.field3 = t1.field3  

See Below, I only scripted a few tables but you can see how to do the rest.

UPDATE FlightBooking set TotalCost = (ExtraCost.SumExtra + SumLuggage)  From FlightBooking 
INNER JOIN Passenger ON Passenger.FlightBookingId=FlightBooking.FlightBookingId 
INNER JOIN AirplaneSeat ON AirplaneSeat.AirplaneSeatId =  Passenger.SeatId
INNER JOIN Section ON AirplaneSeat.SectionId = Section.SectionId
INNER JOIN (Select FlightBookingId, sum(ExtraCost) as SumExtra from Extracost Group by FlightBookingId) as  ExtraCost
     ON ExtraCost.FlightBookingId=FlightBooking.FlightBookingId
INNER JOIN (Select FlightBookingId, sum(Price) as SumLuggage from Luggage Group by FlightBookingId) as  Luggage
     ON Luggage.FlightBookingId=FlightBooking.FlightBookingId
Barmar
  • 741,623
  • 53
  • 500
  • 612
Scott Dobbins
  • 294
  • 1
  • 8
0

Are you sure that you have multiple records that match a single booking id?

If not, you can dispense with the sum():

SET FlightBooking.TotalCost = (Section.PriceInflux+Route.RoutePrice+Luggage.Price+SportsEquipment.SportsEquipmentPrice+Insurance.Price+CarHirePrice.TotalPrice)

If not, you'll need to pre-aggregate the tables that could generate multiple rows. In fact, you need to do this anyway to get a valid result (Cartesian products generated by joins will throw off the over sum).

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

many different possibilities for using aggregate funtion with update and inner join

  UPDATE flight
  SET flight.TotalCost = t.sumPrice
  FROM FlightBooking AS flight
  INNER JOIN
      (
        SELECT    SUM(Section.PriceInflux+Route.RoutePrice+Luggage.Price+SportsEquipment.SportsEquipmentPrice+Insurance.Price+CarHirePrice.TotalPrice)) as sumPrice
        FROM Passenger ON Passenger.FlightBookingId=flight.FlightBookingId 
        INNER JOIN AirplaneSeat ON AirplaneSeat.AirplaneSeatId =  Passenger.SeatId
        INNER JOIN Section ON AirplaneSeat.SectionId = Section.SectionId
        INNER JOIN ExtraCost ON ExtraCost.FlightBookingId=flight.FlightBookingId
        INNER JOIN Luggage ON Luggage.LuggageId = ExtraCost.LuggageId
        INNER JOIN SportsEquipment ON ExtraCost.SportsEquipmentId=SportsEquipment.SportsEquipmentId
        INNER JOIN Insurance ON ExtraCost.InsuranceId = Insurance.InsuranceId
        INNER JOIN CarHirePrice ON CarHirePrice.CarHirePriceId= ExtraCost.CarHirePriceId
        INNER JOIN Route ON flight.RouteId = Route.RouteId
      ) t
     WHERE flight.bookingID = 1

also have a look to this examples PC @PauloSantos and @OMGPonieshttps://stackoverflow.com/a/2009981/4426282

Community
  • 1
  • 1
monikapatelIT
  • 977
  • 14
  • 26