GTFS 获得旅行的最后一站
GTFS get last stop on trip
使用标准 GTFS 数据库,我试图将行程的最后一站名称添加到我当前的查询中,returns 如下:
| trip_id | service_id | departure_stop | departure_time | arrival_stop | arrival_time | end_departure |
|---------------------------------|------------|----------------|----------------|-----------------|--------------|---------------|
| 15693832.T6.2-EPP-E-mjp-1.11.R | T6_1 | Clifton Hill | 04:56:00 | Flinders Street | 05:07:00 | 05:07:00 |
等等。
我当前的查询是:
select `t`.`trip_id`,
`c`.`service_id`,
`start_s`.`stop_name` as `departure_stop`,
`start_st`.`departure_time`,
`end_s`.`stop_name` as `arrival_stop`,
`end_st`.`arrival_time`,
`end_st`.`departure_time` as `end_departure`
from `trips` as `t`
inner join `calendar` as `c` on `t`.`service_id` = `c`.`service_id`
inner join `routes` as `r` on `t`.`route_id` = `r`.`route_id`
inner join `stop_times` as `start_st` on `t`.`trip_id` = `start_st`.`trip_id`
inner join `stops` as `start_s` on `start_st`.`stop_id` = `start_s`.`stop_id`
inner join `stop_times` as `end_st` on `t`.`trip_id` = `end_st`.`trip_id`
inner join `stops` as `end_s` on `end_st`.`stop_id` = `end_s`.`stop_id`
where `start_st`.`departure_time` > '00:00:00'
and `start_st`.`departure_time` < '23:59:59'
and `start_s`.`stop_id` = 19974
and `end_s`.`stop_id` = 19854
and start_st.departure_time < end_st.arrival_time
order by arrival_time asc
我一直在试图弄清楚如何才能在 stop_times
table 中获得最后一站 trip_id
我的查询 returns 每行.
所以除了我目前拥有的,我还想:
- 在
stop_times
table 中获取 trip_id
的最后一个 stop_id
- 从
stops
table得到stop_name
对应的stop_id
- 将其显示为输出中的最后一列
last_stop
更新:
我尝试选择 s.stop_name
并添加以下内部联接:
inner join (
SELECT s.stop_name, trip_id
FROM stop_times
INNER JOIN stops as s on `s`.`stop_id` = `stop_times`.`stop_id`
ORDER BY stop_sequence DESC
) s on `t`.`trip_id` = `s`.`trip_id`
但是,这会为旅行中的每一站添加额外的行,我只想要最后一站并添加 LIMIT 1
returns 没有结果。
请注意,到达和离开时间可能晚于 GTFS 规范中的午夜(小时值可能是 24、25 等)
select "t"."trip_id",
"c"."service_id",
"start_s"."stop_name" as "departure_stop",
"start_st"."departure_time",
"end_s"."stop_name" as "arrival_stop",
"end_st"."arrival_time",
"end_st"."departure_time" as "end_departure",
"last_st"."arrival_time" as "last_arrival",
"last_s"."stop_name" as "last_stop"
from "trips" as "t"
inner join "calendar" as "c" on "t"."service_id" = "c"."service_id"
inner join "routes" as "r" on "t"."route_id" = "r"."route_id"
inner join "stop_times" as "start_st" on "t"."trip_id" = "start_st"."trip_id"
inner join "stops" as "start_s" on "start_st"."stop_id" = "start_s"."stop_id"
inner join "stop_times" as "end_st" on "t"."trip_id" = "end_st"."trip_id"
inner join "stops" as "end_s" on "end_st"."stop_id" = "end_s"."stop_id"
inner join "stop_times" as "last_st" on "t"."trip_id" = "last_st"."trip_id"
inner join "stops" as "last_s" on "last_st"."stop_id" = "last_s"."stop_id"
where "start_s"."stop_id" = '245' -- my data's stop id
and "end_s"."stop_id" = '762' -- my data's stop id
and "last_st"."stop_sequence" = (select max("stop_sequence") from "stop_times" where "t"."trip_id" = "trip_id")
and start_st.departure_time < end_st.arrival_time
order by arrival_time asc
使用标准 GTFS 数据库,我试图将行程的最后一站名称添加到我当前的查询中,returns 如下:
| trip_id | service_id | departure_stop | departure_time | arrival_stop | arrival_time | end_departure |
|---------------------------------|------------|----------------|----------------|-----------------|--------------|---------------|
| 15693832.T6.2-EPP-E-mjp-1.11.R | T6_1 | Clifton Hill | 04:56:00 | Flinders Street | 05:07:00 | 05:07:00 |
等等。
我当前的查询是:
select `t`.`trip_id`,
`c`.`service_id`,
`start_s`.`stop_name` as `departure_stop`,
`start_st`.`departure_time`,
`end_s`.`stop_name` as `arrival_stop`,
`end_st`.`arrival_time`,
`end_st`.`departure_time` as `end_departure`
from `trips` as `t`
inner join `calendar` as `c` on `t`.`service_id` = `c`.`service_id`
inner join `routes` as `r` on `t`.`route_id` = `r`.`route_id`
inner join `stop_times` as `start_st` on `t`.`trip_id` = `start_st`.`trip_id`
inner join `stops` as `start_s` on `start_st`.`stop_id` = `start_s`.`stop_id`
inner join `stop_times` as `end_st` on `t`.`trip_id` = `end_st`.`trip_id`
inner join `stops` as `end_s` on `end_st`.`stop_id` = `end_s`.`stop_id`
where `start_st`.`departure_time` > '00:00:00'
and `start_st`.`departure_time` < '23:59:59'
and `start_s`.`stop_id` = 19974
and `end_s`.`stop_id` = 19854
and start_st.departure_time < end_st.arrival_time
order by arrival_time asc
我一直在试图弄清楚如何才能在 stop_times
table 中获得最后一站 trip_id
我的查询 returns 每行.
所以除了我目前拥有的,我还想:
- 在
stop_times
table 中获取 - 从
stops
table得到stop_name
对应的stop_id
- 将其显示为输出中的最后一列
last_stop
trip_id
的最后一个 stop_id
更新:
我尝试选择 s.stop_name
并添加以下内部联接:
inner join (
SELECT s.stop_name, trip_id
FROM stop_times
INNER JOIN stops as s on `s`.`stop_id` = `stop_times`.`stop_id`
ORDER BY stop_sequence DESC
) s on `t`.`trip_id` = `s`.`trip_id`
但是,这会为旅行中的每一站添加额外的行,我只想要最后一站并添加 LIMIT 1
returns 没有结果。
请注意,到达和离开时间可能晚于 GTFS 规范中的午夜(小时值可能是 24、25 等)
select "t"."trip_id",
"c"."service_id",
"start_s"."stop_name" as "departure_stop",
"start_st"."departure_time",
"end_s"."stop_name" as "arrival_stop",
"end_st"."arrival_time",
"end_st"."departure_time" as "end_departure",
"last_st"."arrival_time" as "last_arrival",
"last_s"."stop_name" as "last_stop"
from "trips" as "t"
inner join "calendar" as "c" on "t"."service_id" = "c"."service_id"
inner join "routes" as "r" on "t"."route_id" = "r"."route_id"
inner join "stop_times" as "start_st" on "t"."trip_id" = "start_st"."trip_id"
inner join "stops" as "start_s" on "start_st"."stop_id" = "start_s"."stop_id"
inner join "stop_times" as "end_st" on "t"."trip_id" = "end_st"."trip_id"
inner join "stops" as "end_s" on "end_st"."stop_id" = "end_s"."stop_id"
inner join "stop_times" as "last_st" on "t"."trip_id" = "last_st"."trip_id"
inner join "stops" as "last_s" on "last_st"."stop_id" = "last_s"."stop_id"
where "start_s"."stop_id" = '245' -- my data's stop id
and "end_s"."stop_id" = '762' -- my data's stop id
and "last_st"."stop_sequence" = (select max("stop_sequence") from "stop_times" where "t"."trip_id" = "trip_id")
and start_st.departure_time < end_st.arrival_time
order by arrival_time asc