简体   繁体   中英

Using SUM function in an update query with inner joins

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.

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

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).

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 @OMGPonies https://stackoverflow.com/a/2009981/4426282

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM