Stephen L
Stephen L

Reputation: 185

How to Select all records, Child as Parent from two tables until no child record as parent , in Oracle?

May be my title description is not clear for you :-) But, below is example.

I have two tables,

Table 1:

SNO   PARENTID   
1      P01
2      P02
3      P03
4      P04

Table 2:

SNO   CHILDID   PARENTID
1      C01        P01
2      C02        P01
3      C03        P03
4      P03        P01
5      P04        P01
6      A01        P03
7      A02        P04

So, each ParentId has multiple childs like

select childid from table2 where parentid = 'P01';

I will get C01,C02,P03 AND P04. This is normal,

But in my case I want to again check these childids with my table1(parent table), if present then again I need to the childids of those parentids also. it will continue until my childid does not match with my parentid.

Ex Cont.. Above my output contains P03 and P04, so I need to take the childids C03,A01 AND A02 also...

I need query for this,help me guys.

Thanks in Advance, Stephen.L

Upvotes: 0

Views: 104

Answers (2)

APC
APC

Reputation: 146229

I'm not quite clear how you want to layout the result set but you should use Oracle's hierarchical query syntax:

SQL> select t2.parentid
  2         , lpad(' ', (level-1)*2)||t2.childid as childid
  3         , level
  4  from t2 
  5  connect by prior t2.childid = t2.parentid
  6  start with t2.parentid = 'P01'
  7  /

PARE CHILDID         LEVEL
---- ---------- ----------
P01  C01                 1
P01  C02                 1
P01  P03                 1
P03    A01               2
P03    C03               2
P01  P04                 1
P04    A02               2

7 rows selected.

SQL> 

Here I have ignored T1 as T2 has the information necessary to create the hierarchy. You can join to T1 in the query, but your question isn't clear what role it plays. By not joining to it we lose the entry for P02. However, joining with T1 will generate multiple rows for P03 and P04.

SQL> select t1.parentid
 2          , lpad(' ', level*2)||t2.childid as childid
 3          , level
 4   from t1
 5      left join t2 on t1.parentid=t2.parentid
 6   connect by prior t2.childid = t2.parentid
 7   start with t1.parentid is not null
 8   order by t1.parentid
 9   /

PARE CHILDID         LEVEL
---- ---------- ----------
P01    C01               1
P01    C02               1
P01    P03               1
P01    P04               1
P02                      1
P03      C03             2
P03    A01               1
P03    C03               1
P03      A01             2
P04      A02             2
P04    A02               1

11 rows selected.

SQL> 

Oracle offers some highly neat extensions to SQL to work with hierarchical data. They are definitely work exploring. Find out more.

Upvotes: 1

Praveen
Praveen

Reputation: 9335

You can try this:

select CHILDID from tbl2 t2 
join tbl1 t1 on t2.PARENTID = t1.PARENTID
START WITH t2.parentid = 'P01'
CONNECT BY NOCYCLE PRIOR CHILDID = t2.parentid;

Here if the CHILDID retrieved from tbl2 doesn't exists in tbl1, then it won't search for its children

Here Oracle hierarchical query is used for more info check this link

Upvotes: 0

Related Questions