如何获得可用座位数量

时间:2013-04-17 15:08:59

标签: sql count

这是表格:
航班(FlightID,MaxCapacity)
FlightBooking(BookingID,FlightID,NumSeats,Status)状态:保留,保持,过期,可以。

我已经知道如何显示预留座位和待机座位,但我在计算可用座位时遇到问题,这些座位应等于MaxCapacity减去预留座位和保持座位。

对此有何建议?

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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