Solved

hierarchical records into rows

Posted on 2013-01-08
3
210 Views
Last Modified: 2013-01-08
Experts,

I have a table with a hierarchy of pharmacy info. It is received on a completely different format than we need.
It is not a fixed amount of levels in the hierarchy. It can be up to 8, but some may only have 2.
The "Drug_Parent" in the drug level one step higher. THe "Ulimate_Parent" is the highest level in that particular hierarchy.
Basically, if the drug has an "Ulitmate Parent" = 0, then that is the top level in the hierarchy.

We've got something to get us started, but we figure that there has to be a better way. Especially if they keep expanding the amount of tiers in the hierarchy.



---raw format
DRUG_ID  DRUG_NAME, DRUG_PARENT,  ULITMATE_PARENT, DRUG_LEVEL
001       PAINKILLER      0            0            1
002           ASPRIN            001            001            2
003      CHILD ASPRIN      002            001            3
007          Anti-Fungal      0            0            1
009         Fungal-Cream      007            007            2      


---Desired Output

DRUG_ID1  DRUG_NAME1  DRUG_ID2  DRUG_NAME2, DRUG_ID3, DRUG_NAME3
001        PAINKILLER  002       ASPRIN                                 003       CHILD ASPRIN
007            anit-Fungal    009       Fungal-Cream                 NULL     NULL
0
Comment
Question by:jvoconnell
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 74

Accepted Solution

by:
sdstuber earned 500 total points
ID: 38755328
SELECT REGEXP_SUBSTR(ids, '[^/]+', 1, 1) drug_id1,
       REGEXP_SUBSTR(names, '[^/]+', 1, 1) drug_name1,
       REGEXP_SUBSTR(ids, '[^/]+', 1, 2) drug_id2,
       REGEXP_SUBSTR(names, '[^/]+', 1, 2) drug_name2,
       REGEXP_SUBSTR(ids, '[^/]+', 1, 3) drug_id3,
       REGEXP_SUBSTR(names, '[^/]+', 1, 3) drug_name3,
       REGEXP_SUBSTR(ids, '[^/]+', 1, 4) drug_id4,
       REGEXP_SUBSTR(names, '[^/]+', 1, 4) drug_name4,
       REGEXP_SUBSTR(ids, '[^/]+', 1, 5) drug_id5,
       REGEXP_SUBSTR(names, '[^/]+', 1, 5) drug_name5,
       REGEXP_SUBSTR(ids, '[^/]+', 1, 6) drug_id6,
       REGEXP_SUBSTR(names, '[^/]+', 1, 6) drug_name6,
       REGEXP_SUBSTR(ids, '[^/]+', 1, 7) drug_id7,
       REGEXP_SUBSTR(names, '[^/]+', 1, 7) drug_name7,
       REGEXP_SUBSTR(ids, '[^/]+', 1, 8) drug_id8,
       REGEXP_SUBSTR(names, '[^/]+', 1, 8) drug_name8
  FROM (SELECT     SYS_CONNECT_BY_PATH(drug_id, '/') ids,
                   SYS_CONNECT_BY_PATH(drug_name, '/') names,
                   CONNECT_BY_ISLEAF isleaf
              FROM yourtable
        CONNECT BY drug_parent = PRIOR drug_id
        START WITH drug_parent = 0)
 WHERE isleaf = 1
0
 
LVL 1

Author Closing Comment

by:jvoconnell
ID: 38755519
AMAZING! I've never been exposed to that before. We were handling this in 100 lines of code that was extremely confusing to read and took longer to run. This works great. I just had to change the '/' to another value due to an ora-30004 error. Thank you so much.
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 38755633
glad I could help
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

737 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