Solved

Usin connect to prior for the hirerarchy query and order by

Posted on 2007-03-20
6
745 Views
Last Modified: 2013-12-19
Hi

I have a table where there have relationships

account_id   related_account_id  relationship_type
---------------------------------------------------
100  110   GP-P
100  120   GP-P
110  130   P-C
120  140   P-C
120  150   P-C
200  210   GP-P
210  220   P-C

I want the data to be returned as first record should be the account_id of grandparent,
next record should be the corresponding parent and next records should be the corresponding child.
Now next record should be the corresponding parent and of the first grand parent and thereafter the corresponding childs of that parent.
Now after these set of records of the first hierarchies next should display the details of second grandparent
ex:
Now the query should return

account_id    relationship_type
-------------------------------
100  Grandparent
110  Parent
130  child
120  Parent
140  child
150  child
200  Grandparent
210  Parent
220  child

I know that i should use CONNECT BY PRIOR can someone help me out.
Iam using oracle 9i.

Thanks in advance

0
Comment
Question by:aaba646
  • 2
6 Comments
 
LVL 22

Expert Comment

by:Helena Marková
ID: 18762247
Have you tried something like this ?
Select account_id,relationship_type from <your_table> connect by prior account_id=related_account_id;
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 20553435
How are you translating GP-P and P-C to Grandparent, Parent and child?

Is that a typo?  Or do you really need your info to translate like that?
0
 
LVL 73

Accepted Solution

by:
sdstuber earned 500 total points
ID: 22205009
here's one last shot at it.

assuming the data is correct, no typos and a simple search and replace with no lookups is fine.

Then this returns the correct results
  SELECT *

    FROM (SELECT CASE WHEN n = 1 THEN account_id ELSE related_account_id END

                     account_id,

                 DECODE(CASE

                            WHEN n = 1

                            THEN

                                SUBSTR(relationship_type,

                                       1,

                                       INSTR(relationship_type, '-') - 1)

                            ELSE

                                SUBSTR(relationship_type,

                                       INSTR(relationship_type, '-') + 1)

                        END,

                        'GP',

                        'Grandparent',

                        'P',

                        'Parent',

                        'C',

                        'child')

                     relationship_type,

                 rn,

                 ROW_NUMBER() OVER (

                              PARTITION BY CASE

                                               WHEN n = 1 THEN account_id

                                               ELSE related_account_id

                                           END

                                  ORDER BY rn)

                     r

            FROM (    SELECT account_id,

                             related_account_id,

                             relationship_type,

                             ROWNUM rn

                        FROM yourtable

                  START WITH account_id NOT IN

                                     (SELECT related_account_id

                                        FROM yourtable)

                  CONNECT BY account_id = PRIOR related_account_id),

                 (SELECT 1 n FROM DUAL

                  UNION ALL

                  SELECT 2 FROM DUAL))

   WHERE r = 1

ORDER BY rn

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Oracle RMAN Database Restore 5 51
T-SQL Convert to PL/SQL 23 76
query returning everything 11 86
How do I call MySQL Stored Procedure from oracle using HS link ? 5 22
Article by: Swadhin
From the Oracle SQL Reference (http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries006.htm) we are told that a join is a query that combines rows from two or more tables, views, or materialized views. This article provides a glimps…
This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.

939 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

2 Experts available now in Live!

Get 1:1 Help Now