如何在MySQL中重新排列表格?

时间:2015-09-30 07:58:04

标签: mysql gtfs

有一个表stop_times.txt,其格式(GTFS)类似于:

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

我期待的结果是:

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

PS:标题可能不准确。请改进它。

还有一个问题:如何将与stop_name对应的stop_sequence添加到此表中?

enter image description here

以下是不正确的代码,因为first_stoplast_stop的停止名称应与不同的stop_id对应:

(SELECT routes.route_short_name, MIN(stop_times.stop_sequence) AS first_stop, stops.stop_name, MAX(stop_times.stop_sequence) AS last_stop, stops.stop_name
FROM 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 BY stop_times.trip_id);

编辑:我在几个小时的工作后完成了。这是密钥源代码:

SELECT T1.trip_id, T1.stop_sequence, T1.stop_id, T2.stop_sequence, T2.stop_id
FROM
    -- create a 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
    FROM stop_times st_first1
    INNER JOIN 
        -- filter out the first stop: trip_id, stop_sequence=0
        (SELECT stop_times.trip_id, MIN(CAST(stop_times.stop_sequence AS UNSIGNED)) AS first_stop
        FROM stop_times
        GROUP BY stop_times.trip_id
        ) st_first2
    ON st_first1.trip_id=st_first2.trip_id AND st_first1.stop_sequence=st_first2.first_stop
    ) T1

LEFT JOIN -- combine T1 and T2

    -- create a 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
    FROM stop_times st_last1
    INNER JOIN
        -- filter out the last stop: trip_id, stop_sequence=MAX
        (SELECT stop_times.trip_id, MAX(CAST(stop_times.stop_sequence AS UNSIGNED)) AS last_stop
        FROM stop_times
        GROUP BY stop_times.trip_id
        ) st_last2
    ON st_last1.trip_id=st_last2.trip_id AND st_last1.stop_sequence=st_last2.last_stop
    ) T2

ON T1.trip_id=T2.trip_id

2 个答案:

答案 0 :(得分:2)

您可以GROUP BY trip_id然后获取MINMAX stop_sequence值,分别获得第一站和最后一站。

SELECT DISTINCT st.trip_id, s.stop_name, t.first_stop, t.last_stop
FROM stop_times st INNER JOIN stops s
ON st.stop_id = s.stop_id
RIGHT JOIN
(
    SELECT trip_id, MIN(stop_sequence) AS first_stop, MAX(stop_sequence) AS last_stop
    FROM stop_times
    GROUP BY trip_id
) t
ON t.trip_id = st.trip_id

答案 1 :(得分:1)

如果您想创建一个新表,那么您可以使用以下查询:

 create table new_table as
SELECT trip_id, MIN(stop_sequence) AS first_stop,MAX(stop_sequence) AS last_stop
FROM stop_times
GROUP BY trip_id