I have an old ugly system table that I need to normalize. It contains a pipe delimited part numbers list in one field and a corresponding node in another column.
Here is an example:
My desired output from a single query would be:
I can parse a single row like I want if I use regexp_substr and connect by like this:
But, if I remove the node = 100 condition, then the results are wrong. I know I could parse through this easily using PL/SQL, but I'd like to see if it's possible with pure SQL first. In case it's not obvious, I'm on Oracle 11g. Thanks for the replies!
Here is an example:
Code:
CREATE TABLE lookup(
part_number VARCHAR2(100),
node NUMBER
);
INSERT
INTO lookup(part_number, node)
VALUES ('aaabbb|aaaccc|aaaddd|aaaeee', 100);
INSERT
INTO lookup(part_number, node)
VALUES ('aaafff', 200);
INSERT
INTO lookup(part_number, node)
VALUES ('aaaggg|aaahhh|aaaiii', 300);
SELECT *
FROM lookup;
Output:
part_number node
aaabbb|aaaccc|aaaddd|aaaeee 100
aaafff 200
aaaggg|aaahhh|aaaiii 300
My desired output from a single query would be:
Code:
part_number node
aaabbb 100
aaaccc 100
aaaddd 100
aaaeee 100
aaafff 200
aaaggg 300
aaahhh 300
aaaiii 300
I can parse a single row like I want if I use regexp_substr and connect by like this:
Code:
SELECT regexp_substr(part_number, regex, 1, LEVEL), node
FROM (SELECT part_number, node, '[^|]+' regex
FROM lookup
WHERE node = 100)
CONNECT BY LEVEL <= LENGTH(REGEXP_REPLACE(part_number, regex)) + 1;
Output:
aaabbb 100
aaaccc 100
aaaddd 100
aaaeee 100
But, if I remove the node = 100 condition, then the results are wrong. I know I could parse through this easily using PL/SQL, but I'd like to see if it's possible with pure SQL first. In case it's not obvious, I'm on Oracle 11g. Thanks for the replies!