我有一张表train_schedule
| id | train_no |train_name | station_no|station_code|arr_time| dep_time | distance|src_station_code|dest_station_code|
-----------------------------------------------------------------------------------------------------------------------------
| 1 | 0123 | ABC | 1 | ROU |00:00:00| 06:00:00 | 0 | ROU | BBS |
| 2 | 0123 | ABC | 2 | CLM |06:50:00| 06:55:00 | 100 | ROU | BBS |
| 3 | 0123 | ABC | 3 | GNT |07:30:00| 07:32:00 | 150 | ROU | BBS |
| 4 | 0123 | ABC | 4 | BBS |08:30:00| 00:00:00 | 300 | ROU | BBS |
-----------------------------------------------------------------------------------------------------------------------------
| 5 | 0224 | XYZ | 1 | HTE |00:00:00| 09:00:00 | 0 | HTE | BBS |
| 6 | 0224 | XYZ | 2 | ROU |09:51:00| 09:56:00 | 200 | HTE | BBS |
| 7 | 0224 | XYZ | 3 | BBS |10:30:00| 00:00:00 | 550 | HTE | BBS |
-----------------------------------------------------------------------------------------------------------------------------
| 8 | 0774 | PQR | 1 | NDLS |00:00:00| 02:00:00 | 0 | NDLS | BNC |
| 9 | 0774 | PQR | 2 | ROU |04:50:00| 04:56:00 | 200 | NDLS | BNC |
| 10 | 0774 | PQR | 3 | BBS |06:30:00| 06:45:00 | 450 | NDLS | BNC |
| 11 | 0774 | PQR | 4 | BNC |10:30:00| 00:00:00 | 550 | NDLS | BNC |
用户将输入作为来源和目的地,例如: ROU 和 BBS
输出将是3次列车进行此选择, train_no,arr_time,dep_time和总距离,我尝试了不同的可能性,能够在火车和目的地之间获得火车,但不能连接列车。
期望的结果:
| id | train_no |train_name | station_code|dep_time| arr_time | distance | FROM | TO |
------------------------------------------------------------------------------------------------------------------
| 1 | 0123 | ABC | ROU |06:00:00| 08:30:00 | 300 | ROU | BBS |
| 2 | 0224 | XYZ | ROU |09:56:00| 10:30:00 |550-200=350| ROU | BBS |
| 3 | 0774 | PQR | ROU |04:56:00| 06:30:00 |450-200=250| ROU | BBS |
我尝试过以下查询虽然它提供了所需的结果,但只有src_station_code和destination_station_code之间的列车我也想要连接列车。
select FirstSet.train_no, FirstSet.total_distance, FirstSet.arrival_time, FirstSet.source_station_code, FirstSet.source_station_name, FirstSet.destination_station_code, FirstSet.destination_station_name, SecondSet.departure_time
from
( SELECT train_no, MAX(distance) AS total_distance, arrival_time,train_name,source_station_code,destination_station_code,source_station_name,destination_station_name FROM train_schedule as q where distance = (select max(distance) from train_schedule where train_no = q.train_no)and (src_stn_code='ROU' and dst_stn_code='BBS') GROUP BY train_no )
as FirstSet left join
( SELECT train_no,source_station_code,departure_time FROM train_schedule where station_no='1' and (src_stn_code='ROU' and dst_stn_code='BBS') ) as SecondSet on FirstSet.train_no = SecondSet.train_no order by FirstSet.train_no
答案 0 :(得分:1)
我已将您的数据和解决方案放在此sqlfiddle。
中以下是您的问题的解决方案SQL(但缺少结果的新ID):
SELECT ts1.train_no,ts1.train_name,ts1.station_code,ts1.dep_time,ts2.arr_time,
ts2.distance -ts1.distance As distance,
ts1.station_code AS 'FROM', ts2.station_code AS 'TO'
FROM train_schedule ts1 JOIN train_schedule ts2
ON ts1.train_no = ts2.train_no AND ts1.station_no < ts2.station_no
WHERE ts1.station_code = 'ROU' AND ts2.station_code = 'BBS';