返回介绍

solution / 2700-2799 / 2793.Status of Flight Tickets / README_EN

发布于 2024-06-17 01:02:59 字数 4033 浏览 0 评论 0 收藏 0

2793. Status of Flight Tickets

中文文档

Description

Table: Flights

+-------------+------+
| Column Name | Type |
+-------------+------+
| flight_id   | int  |
| capacity  | int  |
+-------------+------+
flight_id column contains distinct values.
Each row of this table contains flight id and capacity.

Table: Passengers

+--------------+----------+
| Column Name  | Type   |
+--------------+----------+
| passenger_id | int    |
| flight_id  | int    |
| booking_time | datetime |
+--------------+----------+
passenger_id column contains distinct values.
booking_time column contains distinct values.
Each row of this table contains passenger id, booking time, and their flight id.

Passengers book tickets for flights in advance. If a passenger books a ticket for a flight and there are still empty seats available on the flight, the passenger's ticket will be confirmed. However, the passenger will be on a waitlist if the flight is already at full capacity.

Write a solution to determine the current status of flight tickets for each passenger.

Return the result table ordered by passenger_id _in ascending order._

The result format is in the following example.

 

Example 1:

Input: 
Flights table:
+-----------+----------+
| flight_id | capacity |
+-----------+----------+
| 1     | 2    |
| 2     | 2    |
| 3     | 1    |
+-----------+----------+
Passengers table:
+--------------+-----------+---------------------+
| passenger_id | flight_id | booking_time    |
+--------------+-----------+---------------------+
| 101      | 1     | 2023-07-10 16:30:00 |
| 102      | 1     | 2023-07-10 17:45:00 |
| 103      | 1     | 2023-07-10 12:00:00 |
| 104      | 2     | 2023-07-05 13:23:00 |
| 105      | 2     | 2023-07-05 09:00:00 |
| 106      | 3     | 2023-07-08 11:10:00 |
| 107      | 3     | 2023-07-08 09:10:00 |
+--------------+-----------+---------------------+
Output: 
+--------------+-----------+
| passenger_id | Status  |
+--------------+-----------+
| 101      | Confirmed | 
| 102      | Waitlist  | 
| 103      | Confirmed | 
| 104      | Confirmed | 
| 105      | Confirmed | 
| 106      | Waitlist  | 
| 107      | Confirmed | 
+--------------+-----------+
Explanation: 
- Flight 1 has a capacity of 2 passengers. Passenger 101 and Passenger 103 were the first to book tickets, securing the available seats. Therefore, their bookings are confirmed. However, Passenger 102 was the third person to book a ticket for this flight, which means there are no more available seats. Passenger 102 is now placed on the waitlist, 
- Flight 2 has a capacity of 2 passengers, Flight 2 has exactly two passengers who booked tickets,  Passenger 104 and Passenger 105. Since the number of passengers who booked tickets matches the available seats, both bookings are confirmed.
- Flight 3 has a capacity of 1 passenger. Passenger 107 booked earlier and secured the only available seat, confirming their booking. Passenger 106, who booked after Passenger 107, is on the waitlist.

Solutions

Solution 1

# Write your MySQL query statement below
SELECT
  passenger_id,
  IF(
    (
      RANK() OVER (
        PARTITION BY flight_id
        ORDER BY booking_time
      )
    ) <= capacity,
    'Confirmed',
    'Waitlist'
  ) AS Status
FROM
  Passengers
  JOIN Flights USING (flight_id)
ORDER BY passenger_id;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文