Answered step by step
Verified Expert Solution
Question
1 Approved Answer
WITH RoomAmount AS ( ( SELECT booking.id AS booking _ _ id , , COALESCE ( ( SUM ( ( rc . . base _
WITH RoomAmount AS (
SELECT
booking.id AS booking_id,
COALESCE(SUM(rc.base_price * DATEDIFF(day, booking.checkin_date, booking.checkout_date)),0) AS room_amount
FROM
booking
JOIN
booking_room br ON booking.id = br.booking_id
JOIN
room r ON br.room_id = r.id
JOIN
room_class rc ON r.room_class_id = rc.id
GROUP BY
booking.id
),
AddonAmount AS (
SELECT
booking.id AS booking_id,
COALESCE(SUM(a.price),0) AS addon_amount
FROM
booking
LEFT JOIN
booking_addon ba ON booking.id = ba.booking_id
LEFT JOIN
addon a ON ba.addon_id = a.id
GROUP BY
booking.id
)
UPDATE booking
SET booking_amount =
(
SELECT COALESCE(SUM(room_amount),0)+ COALESCE(SUM(addon_amount),0)
FROM RoomAmount
FULL JOIN AddonAmount ON RoomAmount.booking_id = AddonAmount.booking_id
WHERE booking.id = RoomAmount.booking_id
); can you expalin this query in details ? also is there better way to do it in more comprehensive way ?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Lets break down the query step by step and understand its functionality Additionally Ill provide some suggestions for improvement Query Explanation Co...
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started