2142. The Number of Passengers in Each Bus I

SQL架构

Table: Buses

+--------------+------+
| Column Name  | Type |
+--------------+------+
| bus_id       | int  |
| arrival_time | 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.
No two buses will arrive at the same time.

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 time tbus and a passenger arrived at time tpassenger where tpassenger <= tbus and the passenger did not catch any bus, the passenger will use that 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 |
+--------+--------------+
| 1      | 2            |
| 2      | 4            |
| 3      | 7            |
+--------+--------------+
Passengers table:
+--------------+--------------+
| passenger_id | arrival_time |
+--------------+--------------+
| 11           | 1            |
| 12           | 5            |
| 13           | 6            |
| 14           | 7            |
+--------------+--------------+
Output: 
+--------+----------------+
| bus_id | passengers_cnt |
+--------+----------------+
| 1      | 1              |
| 2      | 0              |
| 3      | 3              |
+--------+----------------+
Explanation: 
- Passenger 11 arrives at time 1.
- Bus 1 arrives at time 2 and collects passenger 11.

- Bus 2 arrives at time 4 and does not collect any passengers.

- 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, 13, and 14.
# Write your MySQL query statement below
with t1 as (select
bus_id,count( passenger_id) passengers_cnt   #  找到 每个客车的 乘客数量
from
(
select
b.bus_id,p.passenger_id,row_number() over(partition by  p.passenger_id order by b.arrival_time ) rn  #找到乘客能赶上的所有车,然后按客车的到达时间排序  后期 取 序号1 就是 每个乘客 乘坐的客车
from
Buses b join Passengers  p 
on b.arrival_time >= p.arrival_time 

) s1
where rn=1
group by bus_id
)
select
bus_id,ifnull(passengers_cnt,0) passengers_cnt 
from
Buses left join t1
using(bus_id)
order by bus_id

开窗:

with a as (
    select bus_id, arrival_time,lag(arrival_time,1) over (order by arrival_time) last_time from buses)
    ,b as (select bus_id,arrival_time,ifnull(last_time,0)last_time from a)
    ,c as (select bus_id,passenger_id from b left join passengers on passengers.arrival_time<=b.arrival_time and passengers.arrival_time>b.last_time)
select bus_id,count(passenger_id) passengers_cnt from c group by bus_id
order by bus_id

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn.linear_model import LogisticRegression '''导入数据并粗略查看情况''' train_data = pd.read_csv(r'C:\Users\86181\Desktop\titanic\train.csv') test_data = pd.read_csv(r'C:\Users\86181\Desktop\titanic\test.csv') print(train_data.head()) print(np.sum(pd.isnull(train_data)))#查看缺失的信息 '''SibSp为兄弟妹的个数,Parch为父母与小孩的个数,Embarked为登船港口''' '''数据清洗''' train_data = train_data.drop(['PassengerId', 'Name', 'Ticket','Cabin'], axis = 1)#删除无关项 test_data = test_data.drop(['PassengerId', 'Name', 'Ticket','Cabin'], axis = 1) print(train_data.head()) train_data = train_data.dropna(axis = 0) print(np.sum(pd.isnull(train_data)))#再次查看是否还有缺失的信息 '''查看数据的总体情况''' train_data['Age'].hist() plt.xlabel('Age') plt.ylabel('Numbers of passengers') plt.title('The age of all passengers') plt.show() train_data['Pclass'].hist() plt.xlabel("'Passengers' class") plt.ylabel('Numbers of passengers') plt.title('The class of all passengers') plt.show() train_data['Sex'].hist() plt.xlabel("Sex") plt.ylabel('Numbers of passengers') plt.title('The sex of all passengers') plt.show() train_data['SibSp'].hist() plt.xlabel("The number of SibSp") plt.ylabel('Numbers of passengers') plt.title('The SibSp of all passengers') plt.show() train_data['Parch'].hist() plt.xlabel("The number of Parch") plt.ylabel('Numbers of passengers') plt.title('The Parch of all passengers') plt.show() train_data['Fare'].hist() plt.xlabel("Fare") plt.ylabel('Numbers of passengers') plt.title('The fare of all passengers') plt.show() train_data['Embarked'].hist() plt.xlabel("Embarked") plt.ylabel('Embarked of passengers') plt.title('The Embarked of all passengers') plt.show() train_data['Survived'].hist() plt.xlabel("Survived") plt.ylabel('Numbers of passengers') plt.title('Survived passengers') plt.show() '''开始分析''' X_train = train_data[['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare', 'Embarked']] Y_train = train_data[['Survived']] X_train = pd.get_dummies(train_data, columns = ['Pclass']) X_train = pd.get_dummies(train_data, columns = ['Embarked']) X_train['Sex'].replace('female', 0, inplace = True) X_train['Sex'].replace('male', 1, inplace = True) print(X_train.head()) print(np.sum(pd.isnull(X_train)))
04-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值