Link to home
Start Free TrialLog in
Avatar of svenerik
svenerik

asked on

SQL query

This may be a simple question, I don't know. I'm a real sql newbie...
Here's the problem:

I have a table for registering paths, and it looks somewhat like this:

tblPath
pathID : NodeID
01 : Node01
01 : Node02
02 : Node01
02 : Node03
03 : Node03
03 : Node04
04 : Node04
04 : Node01

This data illustrates three nodes connected as a triangle like this:
Node1 - Node2 - Node3 - Node04 - Node1
similar to
Node1 -01- Node2 -02- Node3 -03- Node04 -04- Node1

What I would like is to get the pathID of a path providing the names of two nodes.
This way I also want to validate if two provided nodes are connected, ie isConnected(Node2, Node4) will return -1 and isConnected(Node1, Node2) will return 01.

Thanks

Sven-Erik

Avatar of habushad
habushad

If there is only one path possible between two nodes (e.g. there cannot be the following:
01 : Node01
01 : Node02
02 : Node01
02 : Node02
)

Assuming you want to know if Node01 and Node02 are connected, use the following query:

select pathID
from   tblPath t1, tblPath t2
where  t1.NodeId = 'Node01'   and
       t2.pathId = t1.pathId  and
       t2.NodeId = 'Node02'

It uses a technique called "Self Join".
it's not the most efficient way, but it works.
ASKER CERTIFIED SOLUTION
Avatar of nico5038
nico5038
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of svenerik

ASKER

Just what I needed! It works great!
Thanks!

Sven