1

I have a structure that I store equal records in a database table. You can think that these records are siblings. For example I have two records in this table; 1=2 and 1=3. And I need a query that will return all siblings of a given record. Let me give an example;

This is my table with two columns:

create table SIBLINGSTEST(col1 number, col2 number);

I have 2 records, 1=2 and 1=3

insert into SIBLINGSTEST values(1,2);
insert into SIBLINGSTEST values(1,3);

I thought using connect by is the best solution for this situation, and write the following query:

SELECT * FROM SIBLINGSTEST 
START WITH (col1 = 1 or col2 = 1) 
CONNECT BY NOCYCLE (
          (PRIOR col1 = col1) or
          (PRIOR col1 = col2) OR
          (PRIOR col2 = col1) or
          (PRIOR col2 = col2))

This query returns correct results, returning both rows.

If I use 2 as a parameter, the query also runs correctly, returning again both rows.

But if I use 3 as a parameter, the query does not run as I expected, returning only the start row.

SELECT * FROM SIBLINGSTEST 
START WITH (col1 = 3 or col2 = 3) 
CONNECT BY NOCYCLE (
          (PRIOR col1 = col1) or
          (PRIOR col1 = col2) OR
          (PRIOR col2 = col1) or
          (PRIOR col2 = col2))

I wonder why the results of 2 and 3 differs. Any help or idea will be appriciated.

Thanks.

  • I don't get it. What do you call a sibling? When a value in a column occurs more than once? – Thorsten Kettner Jan 15 '15 at 09:53
  • Let me explain it by my example. 1 and 2 are siblings. 1 and 3 are siblings too. So 2 and 3 are siblings too (indirectly). When inserting to my table, I have duplicate value checks, those cases are out of my concern. – Mustafa Şimşek Jan 15 '15 at 10:04
  • And one more thing, the problem occured in our testin db, running oracle 10g version 10.2. And I just tested the situation on an 11g database, there were no problems. The query runs as I expected. I think that's a bug that is fixed with 11g. – Mustafa Şimşek Jan 15 '15 at 10:29

1 Answers1

0

I get both rows with your last query as expected:

SQL> SELECT * FROM SIBLINGSTEST 
  2  START WITH (col1 = 3 or col2 = 3) 
  3  CONNECT BY NOCYCLE (
  4            (PRIOR col1 = col1) or
  5            (PRIOR col1 = col2) OR
  6            (PRIOR col2 = col1) or
  7            (PRIOR col2 = col2));

      COL1       COL2
---------- ----------
         1          3
         1          2

However I would not choose to model it this way. If what you really want is to record that 1, 2, 3 are siblings then I would use:

create table siblings_group (group_id number);
create table people (person_id number, group_id number);
insert into siblings_group values (1);
insert into people values (1, 1);
insert into people values (2, 1);
insert into people values (3, 1);

Then to find all siblings of 3:

SQL>  select person_id from people where group_id =
  2    (select group_id from people where person_id=3);

 PERSON_ID
----------
         1
         2
         3
Tony Andrews
  • 129,880
  • 21
  • 220
  • 259
  • Thanks for your answer. As I mentioned in a comment about, i think that's a bug of oracle. The problem occured in our test db, running oracle 10g version 10.2. And I just tested the situation on an 11g database, there were no problems. The query runs as I expected like yours. I think that's a bug that is fixed with 11g. – Mustafa Şimşek Jan 15 '15 at 12:55
  • And about your structure, yes it's also logical. But your structure does not meet my custumers real requirements. I have splified the problem but actually, we use a similar structure for defining related trainings. For example there are definitions like A=B and B=C and C=D and D=E and goes so on. I can not group these records, because it's expected that if C=D relation is deleted, A and E wont be related anymore. Anyway, my customer is using oracle version 11g, and I have tested that in their enviroment, and runs ok :) Thanks for your answer. – Mustafa Şimşek Jan 15 '15 at 13:07