• 2112. The Airport With the Most Traffic


    SQL架构

    Table: Flights

    +-------------------+------+
    | Column Name       | Type |
    +-------------------+------+
    | departure_airport | int  |
    | arrival_airport   | int  |
    | flights_count     | int  |
    +-------------------+------+
    (departure_airport, arrival_airport) is the primary key column 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 an SQL query 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 query 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.
    1. with t1 as (
    2. select
    3. airport_id,sum(flights_count) flights_count #每个机场的总航班数
    4. from
    5. (
    6. select
    7. departure_airport airport_id,flights_count
    8. from
    9. Flights
    10. union all
    11. select
    12. arrival_airport airport_id,flights_count
    13. from
    14. Flights
    15. )s1
    16. group by
    17. airport_id
    18. )
    19. select
    20. airport_id
    21. from
    22. (
    23. select
    24. airport_id,dense_rank() over( order by flights_count desc) dr #按航班数标号(同数同号)
    25. from
    26. t1
    27. ) s2
    28. where dr = 1 #选出标号为1的就是题中需求

  • 相关阅读:
    【安全边界】
    GO 语言处理并发的时候我们是选择sync还是channel
    在Istio中,到底怎么获取 Envoy 访问日志?
    【Java】Stream规约操作及使用场景
    夜天之书 #67 为什么开源协议不授予商标权利?
    怎样优雅地增删查改(三):业务用户的增删查改
    综合培养学生脑力思维的少儿编程
    恢复出厂设置,手机数据还能“复活”?
    Python 自动化教程(4) : 自动生成PPT文件 Part 2 (干货)
    C语言--求一个 3 X 3 的整型矩阵对角线元素之和
  • 原文地址:https://blog.csdn.net/m0_69157845/article/details/125621956