• 2153. The Number of Passengers in Each Bus II(当时不会)(NO)


    SQL架构

    Table: Buses

    +--------------+------+
    | Column Name  | Type |
    +--------------+------+
    | bus_id       | int  |
    | arrival_time | int  |
    | capacity     | int  |
    +--------------+------+
    bus_id is the primary key column for this table.
    Each row of this table contains information about the arrival time of a bus at the LeetCode station and its capacity (the number of empty seats it has).
    No two buses will arrive at the same time and all bus capacities will be positive integers.
    

    Table: Passengers

    +--------------+------+
    | Column Name  | Type |
    +--------------+------+
    | passenger_id | int  |
    | arrival_time | int  |
    +--------------+------+
    passenger_id is the primary key column for this table.
    Each row of this table contains information about the arrival time of a passenger at the LeetCode station.
    

    Buses and passengers arrive at the LeetCode station. If a bus arrives at the station at a time tbus and a passenger arrived at a time tpassenger where tpassenger <= tbus and the passenger did not catch any bus, the passenger will use that bus. In addition, each bus has a capacity. If at the moment the bus arrives at the station there are more passengers waiting than its capacity capacity, only capacity passengers will use the bus.

    Write an SQL query to report the number of users that used each bus.

    Return the result table ordered by bus_id in ascending order.

    The query result format is in the following example.

    Example 1:

    Input: 
    Buses table:
    +--------+--------------+----------+
    | bus_id | arrival_time | capacity |
    +--------+--------------+----------+
    | 1      | 2            | 1        |
    | 2      | 4            | 10       |
    | 3      | 7            | 2        |
    +--------+--------------+----------+
    Passengers table:
    +--------------+--------------+
    | passenger_id | arrival_time |
    +--------------+--------------+
    | 11           | 1            |
    | 12           | 1            |
    | 13           | 5            |
    | 14           | 6            |
    | 15           | 7            |
    +--------------+--------------+
    Output: 
    +--------+----------------+
    | bus_id | passengers_cnt |
    +--------+----------------+
    | 1      | 1              |
    | 2      | 1              |
    | 3      | 2              |
    +--------+----------------+
    Explanation: 
    - Passenger 11 arrives at time 1.
    - Passenger 12 arrives at time 1.
    - Bus 1 arrives at time 2 and collects passenger 11 as it has one empty seat.
    
    - Bus 2 arrives at time 4 and collects passenger 12 as it has ten empty seats.
    
    - Passenger 12 arrives at time 5.
    - Passenger 13 arrives at time 6.
    - Passenger 14 arrives at time 7.
    - Bus 3 arrives at time 7 and collects passengers 12 and 13 as it has two empty seats.
    1. with t1 as (
    2. select
    3. b.bus_id,
    4. b.capacity,
    5. count(p.passenger_id) cnt #对passeng_id计数,找到 每辆车 最大 有多少人在等待 (不能 对 1 计数 有的车 是会没人等待的)
    6. from
    7. Buses b left join Passengers p
    8. on b.arrival_time >= p.arrival_time
    9. group by b.bus_id,b.capacity
    10. )
    11. select
    12. bus_id,cast(goPassenger as signed) passengers_cnt
    13. from
    14. (
    15. select
    16. bus_id,
    17. @goPassenger := if(capacity >= cnt - @gonePassenger ,cnt - @gonePassenger,capacity) goPassenger, #可以上车的人数
    18. @gonePassenger := @goPassenger + @gonePassenger gonePassenger#已经走掉的人数
    19. from
    20. t1,(select @gonePassenger:=0,@goPassenger:=0) s1 #(初始化变量)
    21. ) s2
    22. order by bus_id

  • 相关阅读:
    云原生时代下DockerFile应用的名场面-尚文网络xUP楠哥
    【leetcode】【剑指offer Ⅱ】048. 序列化与反序列化二叉树
    java开发必备 Git 分支开发:规范指南及完全学会Git的24堂课笔记
    JavaScript问题清单与经验
    python卷积神经网络代码,神经网络代码怎么写
    cola架构:一种扩展点的实现思路浅析
    地理地形sdk:Tatuk GIS Developer Kernel for .NET Crack
    0基础跟我学python---Django(2)
    Calendar日历类
    【数据结构】线性结构——数组、链表、栈和队列
  • 原文地址:https://blog.csdn.net/m0_69157845/article/details/125632702