I have a problem with creating Hierarchical table,
I have the following data
With data(left_code, left_category, right_code, right_category) as (
SELECT '21BEMVXP040150FIS4', 'A', '21CYMVXP040152VFO4', 'B' FROM DUAL UNION ALL
SELECT '21CYMVXP040152VFO4', 'B', '23FRDDS2NCNF1LOBR4', 'C' FROM DUAL UNION ALL
SELECT '22NLDDS2ACNF3MQJC4', 'B', '21BEMVXP040150FIS9', 'A' FROM DUAL UNION ALL
SELECT '21BEMVXP040150FIS9', 'A', '23FRDDS2NCNF1LOBR9', 'C' FROM DUAL UNION ALL
SELECT '21DEMVXP040222UJK5', 'B', '23FRDDS4NCNF1LOBR4', 'C' FROM DUAL UNION ALL
)
SELECT *
FROM data;
I want to connect the left_code with right_code
The desired output should look like this:
| CODE_1 | CATEGORY_1 | CODE_2 | CATEGORY_2 | CODE_3 | CATEGORY_3 |
|---|---|---|---|---|---|
| 21BEMVXP040150FIS4 | A | 21CYMVXP040152VFO4 | B | 23FRDDS2NCNF1LOBR4 | C |
| 22NLDDS2ACNF3MQJC4 | B | 21BEMVXP040150FIS9 | A | 23FRDDS2NCNF1LOBR9 | C |
| 21DEMVXP040222UJK5 | B | 23FRDDS4NCNF1LOBR4 | C | NULL | NULL |
| 22NLDDS2ACNF3MQJC2 | A | 22FRDDS2NCNF1LOBR0 | C | NULL | NULL |
Could you help me please ?
You can use a hierarchical query (which will only query the table once):
or a
LEFT OUTER JOINto the same table (which will query the table twice):Which, for the sample data:
Both output:
fiddle