gtfs - How can I rearrange the table in MySQL? -


there table stop_times.txt format (gtfs) like:

+------------------+---------------+ |     trip_id      | stop_sequence | +------------------+---------------+ | 4503599630773892 |      0        | | 4503599630773892 |      1        | |       ...        |      ...      | | 4503599630773892 |      27       | | 4503599630810392 |      0        | | 4503599630810392 |      1        | |       ...        |      ...      | | 4503599630810392 |      17       | | 4503599631507892 |      0        | | 4503599631507892 |      1        | |       ...        |      ...      | | 4503599631507892 |      29       | |       ...        |      ...      | +------------------+---------------+ 

my expecting result is:

+------------------+------------+-----------+ |     trip_id      | first_stop | last_stop | +------------------+------------+-----------+ | 4503599630773892 |     0      |    27     | | 4503599630810392 |     0      |    17     | | 4503599631507892 |     0      |    19     | |       ...        |    ...     |    ...    | +------------------+------------+-----------+ 

ps: title might not precise. please refine it.


one further question: how can add stop_name corresponds stop_sequence table?

enter image description here

here incorrect code reason stop name of first_stop , last_stop should different corresponding different stop_id:

(select routes.route_short_name, min(stop_times.stop_sequence) first_stop, stops.stop_name, max(stop_times.stop_sequence) last_stop, stops.stop_name stop_times join stops on stops.stop_id=stop_times.stop_id join trips on stop_times.trip_id=trips.trip_id  join routes on routes.route_id=trips.route_id  group stop_times.trip_id); 

edit: make after several hours' work. here key source code:

select t1.trip_id, t1.stop_sequence, t1.stop_id, t2.stop_sequence, t2.stop_id     -- create new table t1: trip_id, stop_sequence=0, stop_id (first stop)     (select st_first1.trip_id, st_first1.stop_sequence, st_first1.stop_id     stop_times st_first1     inner join          -- filter out first stop: trip_id, stop_sequence=0         (select stop_times.trip_id, min(cast(stop_times.stop_sequence unsigned)) first_stop         stop_times         group stop_times.trip_id         ) st_first2     on st_first1.trip_id=st_first2.trip_id , st_first1.stop_sequence=st_first2.first_stop     ) t1  left join -- combine t1 , t2      -- create new table t2: trip_id, stop_sequence=max, stop_id (last stop)     (select st_last1.trip_id, st_last1.stop_sequence, st_last1.stop_id     stop_times st_last1     inner join         -- filter out last stop: trip_id, stop_sequence=max         (select stop_times.trip_id, max(cast(stop_times.stop_sequence unsigned)) last_stop         stop_times         group stop_times.trip_id         ) st_last2     on st_last1.trip_id=st_last2.trip_id , st_last1.stop_sequence=st_last2.last_stop     ) t2  on t1.trip_id=t2.trip_id 

you can group by trip_id , take min , max stop_sequence values obtain first , last stops, respectively.

select distinct st.trip_id, s.stop_name, t.first_stop, t.last_stop stop_times st inner join stops s on st.stop_id = s.stop_id right join (     select trip_id, min(stop_sequence) first_stop, max(stop_sequence) last_stop     stop_times     group trip_id ) t on t.trip_id = st.trip_id 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -