Split table into different level columns

a 夏天 提交于 2019-12-25 09:34:16

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!