0

I have a partner1 <-> partner2 relationship where I have something like this:

(partner1, partner2) 
(1,       2)
(3,       2)
(3,       5)
(4,       5)
(4,       8)
(33,      45)

What I have is one of the partner1 value, and I need to see all the partner1 values in this chain. So, in the above example, I would have partner1=3, and I would like to see the list:

1
3
4

The chain is unlikely to have more than 10 links.

2

1 Answer 1

1

SQL Fiddle

Oracle 11g R2 Schema Setup:

CREATE TABLE partners ( partner1, partner2 ) AS
  SELECT 1,2 FROM DUAL UNION ALL
  SELECT 3,2 FROM DUAL UNION ALL
  SELECT 3,5 FROM DUAL UNION ALL
  SELECT 4,5 FROM DUAL UNION ALL
  SELECT 4,8 FROM DUAL UNION ALL
  SELECT 33,45 FROM DUAL;

Query 1:

SELECT DISTINCT PARTNER1
FROM PARTNERS
START WITH partner1 = 3
CONNECT BY NOCYCLE
             ( MOD( LEVEL, 2 ) = 0 AND PRIOR partner2 = partner2 )
          OR ( MOD( LEVEL, 2 ) = 1 AND PRIOR partner1 = partner1 )

Results:

| PARTNER1 |
|----------|
|        1 |
|        4 |
|        3 |
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.