?
Solved

hierarchical records into rows

Posted on 2013-01-08
3
Medium Priority
?
212 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 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
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.
Suggested Courses

752 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