Home » SQL & PL/SQL » SQL & PL/SQL » Another Hierarchical circular reference question (10.2)
Another Hierarchical circular reference question [message #617779] Thu, 03 July 2014 09:50 Go to next message
lott42_gmail
Messages: 146
Registered: June 2010
Senior Member
I want to display only circular reference problems depending on who is logged in.

data:
with data as
 (select 'a' emp_id, 'c' supv_id
    from dual
  union all
  select 'b' emp_id, 'a' supv_id
    from dual
  union all
  select 'c' emp_id, 'b' supv_id
    from dual
  union all
  select 'e' emp_id, 'g' supv_id
    from dual
  union all
  select 'f' emp_id, 'h' supv_id
    from dual
  union all
  select 'g' emp_id, 'e' supv_id
    from dual
  union all
  select 'h' emp_id, '' supv_id from dual)
select d.*, CONNECT_BY_ISCYCLE
  from data d
 WHERE connect_by_iscycle > 0
CONNECT BY nocycle PRIOR emp_id = supv_id


with this data the results are
EMP_ID	SUPV_ID	CONNECT_BY_ISCYCLE
a	c	1
b	a	1
c	b	1
e	g	1
g	e	1


So if user a,b,c logs in, I want results
EMP_ID	SUPV_ID	CONNECT_BY_ISCYCLE
a	c	1
b	a	1
c	b	1

to come back. and like wise if user e logs in, I want result(s)
EMP_ID	SUPV_ID	CONNECT_BY_ISCYCLE
e	g	1


I tried something like this if login user was 'a'
with data as
 (select 'a' emp_id, 'c' supv_id
    from dual
  union all
  select 'b' emp_id, 'a' supv_id
    from dual
  union all
  select 'c' emp_id, 'b' supv_id
    from dual
  union all
  select 'e' emp_id, 'g' supv_id
    from dual
  union all
  select 'f' emp_id, 'h' supv_id
    from dual
  union all
  select 'g' emp_id, 'e' supv_id
    from dual
  union all
  select 'h' emp_id, '' supv_id from dual)
select *
  from (select d.*, CONNECT_BY_ISCYCLE
          from data d
         WHERE connect_by_iscycle > 0
        CONNECT BY nocycle PRIOR emp_id = supv_id) t
 where emp_id = 'a'
    or supv_id = 'a'

Results
EMP_ID	SUPV_ID	CONNECT_BY_ISCYCLE
a	c	1
b	a	1

Close but NOT exactly what I wanted Mad

Re: Another Hierarchical circular reference question [message #617782 is a reply to message #617779] Thu, 03 July 2014 10:13 Go to previous messageGo to next message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Remove "or supv_id = 'a'", is this not what you want? You didn't say what you want for "a"!

Re: Another Hierarchical circular reference question [message #617783 is a reply to message #617782] Thu, 03 July 2014 10:20 Go to previous messageGo to next message
lott42_gmail
Messages: 146
Registered: June 2010
Senior Member
If either 'a' or 'b' or 'c' logs in, I want

EMP_ID	SUPV_ID
a	c
b	a
c	b


because as far as I can tell ALL 3 are causing the problem and I want to capture that.
Re: Another Hierarchical circular reference question [message #617785 is a reply to message #617783] Thu, 03 July 2014 10:36 Go to previous messageGo to next message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Not clear.
You want the 3 rows for all of these logins or you want the associated row for each login?

Re: Another Hierarchical circular reference question [message #617786 is a reply to message #617785] Thu, 03 July 2014 10:44 Go to previous messageGo to next message
lott42_gmail
Messages: 146
Registered: June 2010
Senior Member
Yes, I want all 3 rows if 'a' logs in or 'b' logs in or 'c' logs in

Login: 'a'
EMP_ID	SUPV_ID
a	c
b	a
c	b


Login: 'b'
EMP_ID	SUPV_ID
a	c
b	a
c	b


Login: 'c'
EMP_ID	SUPV_ID
a	c
b	a
c	b


Re: Another Hierarchical circular reference question [message #617787 is a reply to message #617786] Thu, 03 July 2014 10:58 Go to previous messageGo to next message
lott42_gmail
Messages: 146
Registered: June 2010
Senior Member
Likewise if

Login: 'e'

EMP_ID	SUPV_ID	
e	g


Login: 'h'

"NO RESULTS"


Re: Another Hierarchical circular reference question [message #617790 is a reply to message #617787] Thu, 03 July 2014 12:02 Go to previous messageGo to next message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

SQL> def us=a
SQL> with data as
  2   (select 'a' emp_id, 'c' supv_id
  3      from dual
  4    union all
  5    select 'b' emp_id, 'a' supv_id
  6      from dual
  7    union all
  8    select 'c' emp_id, 'b' supv_id
  9      from dual
 10    union all
 11    select 'e' emp_id, 'g' supv_id
 12      from dual
 13    union all
 14    select 'f' emp_id, 'h' supv_id
 15      from dual
 16    union all
 17    select 'g' emp_id, 'e' supv_id
 18      from dual
 19    union all
 20    select 'h' emp_id, '' supv_id from dual)
 21  select d.emp_id, d.supv_id
 22  from ( select d.*, sys_connect_by_path(emp_id,',')||',' p
 23         from data d
 24         where connect_by_iscycle = 1
 25         CONNECT BY nocycle PRIOR emp_id = supv_id ) d
 26  where d.p like '%,&us.,%'
 27  /
E S
- -
a c
b a
c b

3 rows selected.

SQL> def us=b
SQL> /
E S
- -
a c
b a
c b

3 rows selected.

SQL> def us=c
SQL> /
E S
- -
a c
b a
c b

3 rows selected.

SQL> def us=e
SQL> /
E S
- -
e g
g e

2 rows selected.

SQL> def us=h
SQL> /

no rows selected

Re: Another Hierarchical circular reference question [message #617792 is a reply to message #617790] Thu, 03 July 2014 12:33 Go to previous message
lott42_gmail
Messages: 146
Registered: June 2010
Senior Member
Brilliant!!

Adding the "sys_connect_by_path(emp_id,',')||','" seems to do the trick.

Thanks, Michel!!
Previous Topic: Add sentence if result of string concatenation is too long
Next Topic: Processing data from flat files to base files via staging tables
Goto Forum:
  


Current Time: Fri Apr 26 13:58:34 CDT 2024