Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle - How to use join in hierarchical query and avoid cartesian product

I have a requirement to select data from two tables - first holds hierarchy data (STR table) and another, let's say, starting points of a hierarchy (REQ table) and I need to select hierarchy and combine that with another column from REQ table.

Here is an example:

CREATE TABLE REQ (prop int,          ord  varchar2(10));
CREATE TABLE STR (par  varchar2(10), chld varchar2(10));

INSERT INTO REQ VALUES (100,'A');
INSERT INTO REQ VALUES (101,'A');
INSERT INTO REQ VALUES (102,'B');
INSERT INTO STR VALUES ('A','A1');
INSERT INTO STR VALUES ('A','A2');
INSERT INTO STR VALUES ('A1','A3');
INSERT INTO STR VALUES ('A2','A5');
INSERT INTO STR VALUES ('A3','A6');
INSERT INTO STR VALUES ('B','B1');
INSERT INTO STR VALUES ('B','B2');

Basic query from STR table gives, for example, this hierarchy:

SELECT par, chld, level
FROM STR
CONNECT BY PRIOR chld = par
START WITH PAR IN (SELECT ord FROM REQ WHERE prop = 100);

Hierarhy is OK:

PAR        CHLD            LEVEL
---------- ---------- ----------
A          A1                  1
A1         A3                  2
A3         A6                  3
A          A2                  1
A2         A5                  2

I need to add into result value of column prop from REQ table. I expect to have this result:

PAR        CHLD            LEVEL  PROP 
---------- ---------- ---------- ----- 
A          A1                  1   100   
A1         A3                  2   100
A3         A6                  3   100
A          A2                  1   100
A2         A5                  2   100

When I try to combine both tables in JOIN and CONNECT BY, then I get something like cartesian product, some rows are duplicated:

SELECT STR.par, STR.chld, level, REQ.prop, REQ.ord
FROM STR
,    REQ
WHERE REQ.prop = 100
CONNECT BY PRIOR STR.chld = STR.par
START WITH STR.PAR = REQ.ord;

Result is NOT what I would like to have:

PAR        CHLD            LEVEL       PROP ORD      
---------- ---------- ---------- ---------- ----------
A          A1                  1        100 A         
A1         A3                  2        100 A         
A3         A6                  3        100 A         
A3         A6                  3        100 A        ! extra
A3         A6                  3        100 A        ! rows !
A1         A3                  2        100 A        !
A3         A6                  3        100 A        ! 
A3         A6                  3        100 A        ! 
A3         A6                  3        100 A        ! 
A          A2                  1        100 A         
A2         A5                  2        100 A         
A2         A5                  2        100 A        ! 
12 rows selected 

I there some way to correct the query to get expected data?

like image 272
LiborStefek Avatar asked Sep 18 '25 21:09

LiborStefek


1 Answers

The following query should do the trick - don't know if it's ideal but it will work:

SELECT DISTINCT STR.par, STR.chld, level
     , connect_by_root req.prop AS prop
     , connect_by_root req.ord AS ord
  FROM str
  LEFT JOIN REQ
    ON REQ.ord = str.par
CONNECT BY PRIOR STR.chld = STR.par
  START WITH REQ.prop = 100
like image 167
Radagast81 Avatar answered Sep 20 '25 11:09

Radagast81