问题
I have got a requirement to split the data based on the level into 3 different columns. In the below table SU_DC_1 is the top level based on SETTLEUNIT column I need to add the level.
Parent : TOPSETTLUNIT Child : SETTLEUNIT
Based on Child I need to create 3 additional columns
In the above hierarchy is mentioned as
- Output:
I am finding it difficult to write the query for it. Could you please help .
回答1:
Something like:
SELECT topsettlunit,
settleunit,
CONNECT_BY_ROOT( topsettlunit ) AS level1,
CASE LEVEL
WHEN 1 THEN settleunit
WHEN 2 THEN topsettlunit
END AS level2,
CASE LEVEL
WHEN 2 THEN settleunit
END AS level3
FROM your_table
START WITH topsettlunit NOT IN ( SELECT settleunit FROM your_table )
CONNECT BY PRIOR settleunit = topsettlunit;
来源:https://stackoverflow.com/questions/44903329/split-table-into-different-level-columns