下表是:
Flight (FlightID, MaxCapacity)
FlightBooking (BookingID, FlightID, NumSeats, Status),状态为:Reserved、Held、Expired、Cancall。
我已经知道如何显示保留和保留席位,但我无法计算应等于 MaxCapacity 减去保留和保留席位的可用席位。
请问有什么建议吗?
SELECT f.MaxCapacity - COUNT(b.Status)
FROM Flight f
INNER JOIN FlightBooking b
ON f.FlightID = b.FlightID
WHERE b.Status IN ('Reserved','Held')
GROUP BY f.FlightID
select flightid,
max(f.maxcapacity) - count(case when fb.status in ('reserved', 'held')
then 1
else 0
end)
from flight f
left outer join flightbooking fb on fb.flightid = f.flightid
group by f.flightid