3

我正在尝试构建一个简单的路线查找器,该路线查找器计算并存储路线从 A 到 B 所经过的节点。我有两个表;一个由阶段(节点及其“下一个可能的跃点”)和一个 route_stage 表组成,该表应该能够存储使用唯一路由 ID 计算的每个路由。

舞台表

STAGEID START_STATION                  NEXT_HOP_STATION                   LENGTH
---------- ------------------------------ ------------------------------ ----------
     1 Penzance                       Plymouth                               78 
     2 Plymouth                       Exeter                               44.8 
     3 Exeter                         Taunton                              36.6 
     4 Exeter                         Salisbury                            96.6 
     5 Salisbury                      Basingstoke                          38.2 
     6 Basingstoke                    Southampton                          52.7 
     7 Southampton                    Poole                                  37 
     8 Poole                          Weymouth                             31.6 
     9 Taunton                        Reading                              99.5 
    10 Reading                        Basingstoke                            18 
    11 Reading                        Paddington                           40.9 
    12 Taunton                        Bristol                              48.8 
    13 Bristol                        Bath                                   13 
    14 Bath                           Swindon                              37.5 
    15 Swindon                        Reading                              39.8

Route_Stage 表

ROUTEID    STAGEID
---------- ----------
     1          1 
     1          2 
     1          3 
     1          9 
     1         11 
     2          6 
     2          7 
     2          8 
     2         10 
     2         11 

对于上述情况,ID 为 1 的路线从 Penzance 开始,经过 Plymouth、Exeter、Taunton、Reading 并在 Paddington 终止。理想情况下,我想创建一个存储过程,该过程采用起点和终点站的入口参数,以便内部的代码能够计算出合适的路线。

我看过递归,但有点迷茫,因为我不确定当一个节点有多个潜在路径时代码应该如何反应?它怎么知道哪一个是正确的下去。

任何帮助是极大的赞赏。谢谢!

4

1 回答 1

1

对于一个给定的起始位置,这将(我认为..抱歉,在 iPad 上手动输入)为离开该起始点的每条路线提供一行。

  SELECT
    LEVEL as route_step,
    t1.next_hop_station as next_station,
    t1.stageid

  FROM 
    stage t1

    INNER  JOIN stage t2 
    ON t2.start_station = t1.next_hop_station

  START WITH
    t1.start_station = 'your start station'

  CONNECT BY 
    PRIOR t1.start_station = t1.next_hop_station

所以,对于起点站彭赞斯:

Route_Step  Next_Station StageID
1.          Plymouth.    1
2.          Exeter.      2
3.          Taunton.     3
4.          Reading.     9
5.          Basingstoke. 10
6.          Southampton  6
7.          Poole.       7
8.          Weymouth     8
5.          Paddington.  11
3.          Salisbury    4
4.          Basingstoke. 5
5.          Southampton. 6
6.          Poole.       7
7.          Weymouth.    8

* excuse the .'s!

在不同的起始站上加入连接(并删除显式 START WITH 子句,以便您从所有站获取路线,而不仅仅是单个站)将为您提供输出表所需的内容(尽管根据之前的评论,我不确定该结构对您有什么用,因为您丢失了相关细节):

SELECT
     First_Stage.stageid as routeid,
     q.stageid

   FROM
   (

      SELECT
        LEVEL as route_step,
        t1.next_hop_station as next_station,
        t1.stageid

      FROM 
        stage t1

        INNER  JOIN stage t2 
        ON t2.start_station = t1.next_hop_station

      CONNECT BY 
        PRIOR t1.start_station = t1.next_hop_station
    ) q

    INNER JOIN stage as first_stage
    ON first_stage.stageid = q.stageid
    AND q.route_step = 1
于 2013-04-14T03:36:00.037 回答