SQL 了解如何根据 GTFS 数据中的 station_id 获取下一站
SQL for how to Get next stations based on station_id in GTFS data
这是我的SQL查询
SELECT
Routes.route_id, Routes.route_desc, Routes.route_type,
Trips.service_id, Trips.trip_id, Trips.route_direction,
Stop_times.stop_id, Stop_times.arrival_time, Stop_times.departure_time,
Stops.stop_name, Calendar.start_date, Calendar.end_date
FROM Trips
INNER JOIN Routes ON Trips.route_id = Routes.route_id
INNER JOIN Stop_times ON Trips.trip_id = Stop_times.trip_id
INNER JOIN Stops ON Stop_times.stop_id = Stops.stop_id
INNER JOIN Calendar ON Trips.service_id = Calendar.service_id
WHERE
Calendar.start_date = strftime('%Y%m%d','now') and
CAST(REPLACE(Stop_times.arrival_time, ':', '') as decimal)
>= strftime('%H%M%S', 'now', 'localtime')
and Trips.direction_id = 0
and Stop_times.stop_id = '279018'
ORDER BY Stop_times.arrival_time asc
1> 我的结果是这样的
(还有许多其他列)
2>我想使用第一行 trip_id 并将其作为
SELECT
Routes.route_id, Routes.route_desc, Routes.route_type,
Trips.service_id, Trips.trip_id, Trips.route_direction,
Stop_times.stop_id, Stop_times.arrival_time, Stop_times.departure_time,
Stops.stop_name, Calendar.start_date, Calendar.end_date
FROM Trips
INNER JOIN Routes ON Trips.route_id = Routes.route_id
INNER JOIN Stop_times ON Trips.trip_id = Stop_times.trip_id
INNER JOIN Stops ON Stop_times.stop_id = Stops.stop_id
INNER JOIN Calendar ON Trips.service_id = Calendar.service_id
WHERE
Calendar.start_date = strftime('%Y%m%d','now') and
CAST(REPLACE(Stop_times.arrival_time, ':', '') as decimal)
>= strftime('%H%M%S', 'now', 'localtime')
and Trips.direction_id = 0
and Stop_times.trip_id = 'what ever on first row from above query example 551.22018xxxxx'
ORDER BY Stop_times.arrival_time asc
结果应该如下
提前致谢。
您可以使用 subquery
包装您的结果并使用该结果执行您的过滤器。
您的查询应该是这样的:
SELECT *
FROM(
SELECT
Routes.route_id, Routes.route_desc, Routes.route_type,
Trips.service_id, Trips.trip_id, Trips.route_direction,
Stop_times.stop_id, Stop_times.arrival_time, Stop_times.departure_time,
Stops.stop_name, Calendar.start_date, Calendar.end_date
FROM Trips
INNER JOIN Routes ON Trips.route_id = Routes.route_id
INNER JOIN Stop_times ON Trips.trip_id = Stop_times.trip_id
INNER JOIN Stops ON Stop_times.stop_id = Stops.stop_id
INNER JOIN Calendar ON Trips.service_id = Calendar.service_id
WHERE
Calendar.start_date = strftime('%Y%m%d','now') and
CAST(REPLACE(Stop_times.arrival_time, ':', '') as decimal)
>= strftime('%H%M%S', 'now', 'localtime')
and Trips.direction_id ={}
and Stops_Time.Stop_ID = 'xxxx'
ORDER BY Stop_times.arrival_time ASC)
WHERE Trip_ID = @Trip_ID
编辑:
你只需要在 Stop_Times.Stop_ID
中的第一行,所以先用你的 select 包装相同的条件,然后添加 limit
SELECT
Routes.route_id, Routes.route_desc, Routes.route_type,
Trips.service_id, Trips.trip_id, Trips.route_direction,
Stop_times.stop_id, Stop_times.arrival_time, Stop_times.departure_time,
Stops.stop_name, Calendar.start_date, Calendar.end_date
FROM Trips
INNER JOIN Routes ON Trips.route_id = Routes.route_id
INNER JOIN Stop_times ON Trips.trip_id = Stop_times.trip_id
INNER JOIN Stops ON Stop_times.stop_id = Stops.stop_id
INNER JOIN Calendar ON Trips.service_id = Calendar.service_id
WHERE
Calendar.start_date = strftime('%Y%m%d','now') and
CAST(REPLACE(Stop_times.arrival_time, ':', '') as decimal)
>= strftime('%H%M%S', 'now', 'localtime')
and Trips.direction_id ={}
and Stop_Times.Trip_ID =
(SELECT Stop_Times.Trip_ID
FROM Trips
INNER JOIN Stop_times ON Trips.trip_id = Stop_times.trip_id
INNER JOIN Stops ON Stop_times.stop_id = Stops.stop_id
INNER JOIN Calendar ON Trips.service_id = Calendar.service_id
WHERE
Calendar.start_date = strftime('%Y%m%d','now') and
CAST(REPLACE(Stop_times.arrival_time, ':', '') as decimal)
>= strftime('%H%M%S', 'now', 'localtime')
and Trips.direction_id ={}
ORDER BY Stop_times.arrival_time ASC LIMIT 1)
ORDER BY Stop_times.arrival_time ASC
这是我的SQL查询
SELECT
Routes.route_id, Routes.route_desc, Routes.route_type,
Trips.service_id, Trips.trip_id, Trips.route_direction,
Stop_times.stop_id, Stop_times.arrival_time, Stop_times.departure_time,
Stops.stop_name, Calendar.start_date, Calendar.end_date
FROM Trips
INNER JOIN Routes ON Trips.route_id = Routes.route_id
INNER JOIN Stop_times ON Trips.trip_id = Stop_times.trip_id
INNER JOIN Stops ON Stop_times.stop_id = Stops.stop_id
INNER JOIN Calendar ON Trips.service_id = Calendar.service_id
WHERE
Calendar.start_date = strftime('%Y%m%d','now') and
CAST(REPLACE(Stop_times.arrival_time, ':', '') as decimal)
>= strftime('%H%M%S', 'now', 'localtime')
and Trips.direction_id = 0
and Stop_times.stop_id = '279018'
ORDER BY Stop_times.arrival_time asc
1> 我的结果是这样的
(还有许多其他列)
2>我想使用第一行 trip_id 并将其作为
SELECT
Routes.route_id, Routes.route_desc, Routes.route_type,
Trips.service_id, Trips.trip_id, Trips.route_direction,
Stop_times.stop_id, Stop_times.arrival_time, Stop_times.departure_time,
Stops.stop_name, Calendar.start_date, Calendar.end_date
FROM Trips
INNER JOIN Routes ON Trips.route_id = Routes.route_id
INNER JOIN Stop_times ON Trips.trip_id = Stop_times.trip_id
INNER JOIN Stops ON Stop_times.stop_id = Stops.stop_id
INNER JOIN Calendar ON Trips.service_id = Calendar.service_id
WHERE
Calendar.start_date = strftime('%Y%m%d','now') and
CAST(REPLACE(Stop_times.arrival_time, ':', '') as decimal)
>= strftime('%H%M%S', 'now', 'localtime')
and Trips.direction_id = 0
and Stop_times.trip_id = 'what ever on first row from above query example 551.22018xxxxx'
ORDER BY Stop_times.arrival_time asc
结果应该如下
提前致谢。
您可以使用 subquery
包装您的结果并使用该结果执行您的过滤器。
您的查询应该是这样的:
SELECT *
FROM(
SELECT
Routes.route_id, Routes.route_desc, Routes.route_type,
Trips.service_id, Trips.trip_id, Trips.route_direction,
Stop_times.stop_id, Stop_times.arrival_time, Stop_times.departure_time,
Stops.stop_name, Calendar.start_date, Calendar.end_date
FROM Trips
INNER JOIN Routes ON Trips.route_id = Routes.route_id
INNER JOIN Stop_times ON Trips.trip_id = Stop_times.trip_id
INNER JOIN Stops ON Stop_times.stop_id = Stops.stop_id
INNER JOIN Calendar ON Trips.service_id = Calendar.service_id
WHERE
Calendar.start_date = strftime('%Y%m%d','now') and
CAST(REPLACE(Stop_times.arrival_time, ':', '') as decimal)
>= strftime('%H%M%S', 'now', 'localtime')
and Trips.direction_id ={}
and Stops_Time.Stop_ID = 'xxxx'
ORDER BY Stop_times.arrival_time ASC)
WHERE Trip_ID = @Trip_ID
编辑:
你只需要在 Stop_Times.Stop_ID
中的第一行,所以先用你的 select 包装相同的条件,然后添加 limit
SELECT
Routes.route_id, Routes.route_desc, Routes.route_type,
Trips.service_id, Trips.trip_id, Trips.route_direction,
Stop_times.stop_id, Stop_times.arrival_time, Stop_times.departure_time,
Stops.stop_name, Calendar.start_date, Calendar.end_date
FROM Trips
INNER JOIN Routes ON Trips.route_id = Routes.route_id
INNER JOIN Stop_times ON Trips.trip_id = Stop_times.trip_id
INNER JOIN Stops ON Stop_times.stop_id = Stops.stop_id
INNER JOIN Calendar ON Trips.service_id = Calendar.service_id
WHERE
Calendar.start_date = strftime('%Y%m%d','now') and
CAST(REPLACE(Stop_times.arrival_time, ':', '') as decimal)
>= strftime('%H%M%S', 'now', 'localtime')
and Trips.direction_id ={}
and Stop_Times.Trip_ID =
(SELECT Stop_Times.Trip_ID
FROM Trips
INNER JOIN Stop_times ON Trips.trip_id = Stop_times.trip_id
INNER JOIN Stops ON Stop_times.stop_id = Stops.stop_id
INNER JOIN Calendar ON Trips.service_id = Calendar.service_id
WHERE
Calendar.start_date = strftime('%Y%m%d','now') and
CAST(REPLACE(Stop_times.arrival_time, ':', '') as decimal)
>= strftime('%H%M%S', 'now', 'localtime')
and Trips.direction_id ={}
ORDER BY Stop_times.arrival_time ASC LIMIT 1)
ORDER BY Stop_times.arrival_time ASC