Link to home
Start Free TrialLog in
Avatar of abhitlya
abhitlya

asked on

Need help with a recursive type of query

Hello...

I have a table TEST with 5 numeric columns namely, SRC, F, L, R, U

Each row in this table represents a direct link between SRC and the other columns (F, L, R, U). A value of 0 means no link exists. The values of F, L, R, U may (or may not) be the SRC in subsequent rows. In case F/L/R/U does not appear as a value of SRC, it indicates last node.

Here are some sample values from this table (I have padded them with leading 0 for the sake of alignment):

SRC  F     L     R    U
---   ---   ---   ---   ---
 01   04   00   02   20
 02   00   00   00   03
 03   04   00   00   00
 04   05   00   00   00
 05   08   00   06   00
 08   00   00   09   00
 09   00   10   00   00
 10   00   00   11   00
 11   00   00   00   12
 12   00   00   13   00
 13   00   00   14   00

Now the way one would read this table is...

SRC 1 is connected to F:4, R:2 and U:20
SRC 2 is connected to U:3
SRC 3 is connected to F:4

...and so on.

Now the query I need is for the following:

Given a value for SRC and any other value that would be present in either F, L, R or U, I need to find the possible paths.

For example, if SRC = 1 and we need to reach to 11 then we would have the following traversal paths:

1 - 4 - 5 - 8 - 9 - 10 - 11
1 - 2 - 3 - 4 - 5 - 8 - 9 - 10 - 11

I don't think it would be possible with a single query and hence we may use a table to hold the results, and this table would look something like:

For path 1 - 4 - 5 - 8 - 9 - 10 - 11

SRC  PATH  DEST
----- -------- -------
  1       1        4
  1       1        5
  1       1        8
  1       1        9
  1       1        10
  1       1        11

For path 1 - 2 - 3 - 4 - 5 - 8 - 9 - 10 - 11

SRC  PATH  DEST
----- -------- -------
  1       2        2
  1       2        3
  1       2        4
  1       2        5
  1       2        8
  1       2        9
  1       2        10
  1       2        11

Thanks,
Abhijit
Avatar of earth man2
earth man2
Flag of United Kingdom of Great Britain and Northern Ireland image

Try the hier patch.

This enables an Oracle like connect by prior hierarchical query syntax

http://gppl.moonbone.ru/hier-Pg8.1.2-0.5.5.diff.gz
ASKER CERTIFIED SOLUTION
Avatar of grant300
grant300

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