返回介绍

solution / 2100-2199 / 2112.The Airport With the Most Traffic / README_EN

发布于 2024-06-17 01:03:09 字数 3735 浏览 0 评论 0 收藏 0

2112. The Airport With the Most Traffic

中文文档

Description

Table: Flights

+-------------------+------+
| Column Name     | Type |
+-------------------+------+
| departure_airport | int  |
| arrival_airport   | int  |
| flights_count   | int  |
+-------------------+------+
(departure_airport, arrival_airport) is the primary key column (combination of columns with unique values) for this table.
Each row of this table indicates that there were flights_count flights that departed from departure_airport and arrived at arrival_airport.

 

Write a solution to report the ID of the airport with the most traffic. The airport with the most traffic is the airport that has the largest total number of flights that either departed from or arrived at the airport. If there is more than one airport with the most traffic, report them all.

Return the result table in any order.

The result format is in the following example.

 

Example 1:

Input: 
Flights table:
+-------------------+-----------------+---------------+
| departure_airport | arrival_airport | flights_count |
+-------------------+-----------------+---------------+
| 1         | 2         | 4       |
| 2         | 1         | 5       |
| 2         | 4         | 5       |
+-------------------+-----------------+---------------+
Output: 
+------------+
| airport_id |
+------------+
| 2      |
+------------+
Explanation: 
Airport 1 was engaged with 9 flights (4 departures, 5 arrivals).
Airport 2 was engaged with 14 flights (10 departures, 4 arrivals).
Airport 4 was engaged with 5 flights (5 arrivals).
The airport with the most traffic is airport 2.

Example 2:

Input: 
Flights table:
+-------------------+-----------------+---------------+
| departure_airport | arrival_airport | flights_count |
+-------------------+-----------------+---------------+
| 1         | 2         | 4       |
| 2         | 1         | 5       |
| 3         | 4         | 5       |
| 4         | 3         | 4       |
| 5         | 6         | 7       |
+-------------------+-----------------+---------------+
Output: 
+------------+
| airport_id |
+------------+
| 1      |
| 2      |
| 3      |
| 4      |
+------------+
Explanation: 
Airport 1 was engaged with 9 flights (4 departures, 5 arrivals).
Airport 2 was engaged with 9 flights (5 departures, 4 arrivals).
Airport 3 was engaged with 9 flights (5 departures, 4 arrivals).
Airport 4 was engaged with 9 flights (4 departures, 5 arrivals).
Airport 5 was engaged with 7 flights (7 departures).
Airport 6 was engaged with 7 flights (7 arrivals).
The airports with the most traffic are airports 1, 2, 3, and 4.

Solutions

Solution 1

# Write your MySQL query statement below
WITH
  T AS (
    SELECT * FROM Flights
    UNION
    SELECT arrival_airport, departure_airport, flights_count FROM Flights
  ),
  P AS (
    SELECT departure_airport, SUM(flights_count) AS cnt
    FROM T
    GROUP BY 1
  )
SELECT departure_airport AS airport_id
FROM P
WHERE cnt = (SELECT MAX(cnt) FROM P);

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

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

发布评论

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