Solved

hierarchical records into rows

Posted on 2013-01-08
3
207 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
  • 2
3 Comments
 
LVL 73

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 73

Expert Comment

by:sdstuber
ID: 38755633
glad I could help
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
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…
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

920 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

15 Experts available now in Live!

Get 1:1 Help Now