我正在尝试构建一个简单的路径查找器,它计算并存储路径遍历从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的路线从彭赞斯开始,穿越,普利茅斯,埃克塞特,汤顿,雷丁,并在帕丁顿终止。理想情况下,我想创建一个存储过程,它接受起始和终止站的入口参数,以便内部代码能够计算合适的路由。
我看过递归但有点丢失,因为我不确定当节点有多个潜在路径时代码应该如何反应?怎么会知道哪一个是正确的。
非常感谢任何帮助。谢谢!
答案 0 :(得分: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
所以,对于起始站Penzance:
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