0
CREATE TABLE Flight 
(
    FlightID INTEGER NOT NULL,
    FlightDate TIMESTAMP NOT NULL, 
    Origin VARCHAR(30) NOT NULL,
    Destination VARCHAR(30) NOT NULL,
    MaxCapacity INTEGER NOT NULL,
    PricePerSeat DECIMAL NOT NULL

)
CREATE TABLE FlightBooking 
(
    BookingID INTEGER NOT NULL,
    CustomerID INTEGER NOT NULL,
    FlightID INTEGER NOT NULL, 
    NumSeats INTEGER NOT NULL,
    Status CHAR(1) NOT NULL,
    BookingTime TIMESTAMP NOT NULL,
    TotalCost DECIMAL

)
status = Reserve, Held Cancelled or Expired (only reserved and held are taken into account)
CREATE TABLE SeatBooking
(
    BookingID INTEGER NOT NULL,
    PassengerID INTEGER NOT NULL,
    SeatNumber CHAR(4) 

)

如果给出了flightid以及可用座位总数、保留座位总数和保留座位总数,我是否可以获取座位状态?

4

2 回答 2

0

Given the flight ID, you should be able to get the total available seats, total reserved seats and total held seats like so:

select MaxCapacity - Reserved - Held as Available, Reserved, Held
from
(select f.MaxCapacity,
        sum(case b.status when 'R' then b.NumSeats end) Reserved,
        sum(case b.status when 'H' then b.NumSeats end) Held
 from Flight f
 left join FlightBooking b on f.FlightID = b.FlightID
 where f.FlightID = ?) sq
于 2013-04-10T18:18:16.820 回答
0

如果给出了flightid以及可用座位总数、保留座位总数和保留座位总数,我是否可以获取座位状态?

就在这里!我假设使用 Oracle、SQL Server 或 DB2 并使用 CTE。如果您没有其中之一,则必须使用子查询或临时表。

如果“R”是状态代码,则总保留座位数(如果您想在 where 子句中将其限制为单个航班,但这些查询会为您提供所有航班 ID 的值):

SELECT SUM(NumSeats), FlightID
FROM FlightBooking 
WHERE Status = 'R'
GROUP BY FlightID

如果“H”是状态代码,则持有的总座位数:

SELECT SUM(NumSeats), FlightID
FROM FlightBooking 
WHERE Status = 'H'
GROUP BY FlightID

总可用座位数为:

  WITH used AS
   (
      SELECT SUM(NumSeats) as used, FlightID
      FROM FlightBooking 
      WHERE Status in ('R','H')
      GROUP BY FlightID
   )
   SELECT F.MaxCapacity-U.Used, F.FlightID
   FROM used U
   JOIN Flight F ON U.FlightID = F.FlightID
于 2013-04-10T16:41:00.733 回答