I need some advise because I am very new to hierarchical queries :( I.e. I have the following table:
CREATE TABLE routes
(
from VARCHAR2(15),
to VARCHAR2(15),
price NUMBER
);
INSERT INTO routes VALUES('San Francisco', 'Denver', 1000);
INSERT INTO routes VALUES('San Francisco', 'Dallas', 10000);
INSERT INTO routes VALUES('Denver', 'Dallas', 500);
INSERT INTO routes VALUES('Denver', 'Chicago', 2000);
INSERT INTO routes VALUES('Dallas', 'Chicago', 600);
INSERT INTO routes VALUES('Dallas', 'New York', 2000);
INSERT INTO routes VALUES('Chicago', 'New York', 3000);
INSERT INTO routes VALUES('Chicago', 'Denver', 2000);
I want to calculate the price through the hierarchy, to get the following result:
FROM TO PRICE
--------------- --------------- -----
San Francisco Dallas 10000 //San Francisco -> Dallas
San Francisco Denver 1000 //San Francisco -> Denver
San Francisco Chicago 10600 //San Francisco --> Dallas --> Chicago (10000 + 600)
San Francisco New York 12000 //San Francisco --> Dallas --> New York (10000 + 200)
San Francisco Chicago 3000 //San Francisco --> Denver --> Chicago (1000 + 2000)
San Francisco Dallas 1500 //San Francisco --> Denver --> Dallas (1000 + 500)
. . .
. . .
. . .
I've alreary imagined, that the CONNECT BY PRIOR statement should be used and have written a query which runs through the hierarchy:
SELECT
CONNECT_BY_ROOT from,
to
FROM routes
CONNECT BY NOCYCLE PRIOR to = from;
I really could use some help how to get the prices.
Than You!
Much more straightforward using recursive sql (changed from/to to f/t to avoid using keywords)
RESULTS: