Solved

hierarchical records into rows

Posted on 2013-01-08
3
211 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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 how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

717 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